最近作項目的時候遇到了一個問題,就是在ListView的item上面含有一個EditText,要求是這樣: 1當點擊item的時候,item能夠點擊; 2當點擊EditText的時候EditText也能夠正常彈出軟件盤; 當時真是把我給搞瘋了,item能夠點擊的時候,EditText不能正常使用了,EditText好了的時候,item又不可點擊了,當時那個火,心中一萬句草泥馬都出來了。言歸正傳,先看一下解決的效果:
當一個ListView的item中含有搶佔焦點的控件的時候,咱們一般在item的佈局中加上這樣一句:
android:descendantFocusability=」blocksDescendants」
防止搶佔焦點,大多數狀況下都可以解決問題,可是若是是EditText的時候,就很坑了,你會發現軟件盤都不可以彈出,更別說輸入文字了,要解決這個問題,就是讓點擊EditText的時候,item最外層不攔截點擊事件就ok了,點解item的時候,在攔截點擊事件也一樣ok了。看getView中關鍵代碼片斷:java
mHolder.edtInput.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v.getParent()) .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); return false; } });
這個是EditText的時候的處理
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
這個很關鍵,並且是在OnTouchListener中執行的,由於在Android中View的觸摸事件傳遞中,View的OnTouchListener調用是早於其餘事件的,並且返回值false,表明目前事件沒有被消耗,繼續向下傳遞,不影響後續事件的處理(這裏不影響EditText其餘事件的處理)android
convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v) .setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); Toast.makeText(mContext, "點擊了item", Toast.LENGTH_LONG).show(); return false; } });
這個是convertView的處理,一樣也是一行關鍵代碼
.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
下面附上適配器中的代碼和佈局代碼,須要的同窗本身copy一下看看吧:
item佈局代碼:ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:orientation="horizontal" > <TextView android:id="@+id/tvGroupTitle" android:layout_width="0dip" android:layout_height="50dip" android:layout_weight="1" android:gravity="center" /> <EditText android:id="@+id/edtGroupContent" android:layout_width="100dip" android:layout_height="wrap_content" /> </LinearLayout>
BaseAdapter的代碼:佈局
public class NormalAdapter extends ArrayAdapter<String> { private List<String> mDatas; private Context mContext; public NormalAdapter(Context context, List<String> objects) { super(context, R.layout.activity_list, objects); mDatas = objects; mContext = context; } @Override public View getView(final int position, View convertView, final ViewGroup parent) { Log.e("tag", parent.toString()); Holder mHolder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.item_group, null); mHolder = new Holder(); mHolder.tvTitle = (TextView) convertView .findViewById(R.id.tvGroupTitle); mHolder.edtInput = (EditText) convertView .findViewById(R.id.edtGroupContent); convertView.setTag(mHolder); } else { mHolder = (Holder) convertView.getTag(); } mHolder.tvTitle.setText(mDatas.get(position)); mHolder.edtInput.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v.getParent()) .setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); return false; } }); convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ((ViewGroup) v) .setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); Toast.makeText(mContext, "點擊了item", Toast.LENGTH_LONG).show(); return true; } }); return convertView; } private static final class Holder { TextView tvTitle; EditText edtInput; } }
看看代碼是否是解決問題了,能夠的話,就給個贊吧!哈哈-^–^spa