Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

android - Focusable EditText in the ListView and onItemClick

In each ListView item I have EditText.

When I set android:focusable="false" for EditText then onItemClick on the ListView item is working, but EditText doesn't get cursor when I click inside.

If I'll set android:focusable="true" for EditText, then EditText is focusable, but onItemClick for the ListView doesn't work when I click on it.

How to separate onItemClick and focusable EditText in this item?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks @user370305 for the idea with OnTouchListener. Now it is working for me by using setOnTouchListener():

public class AdapterListCards extends CursorAdapter implements View.OnTouchListener {
 public AdapterListCards(Context context) {
    super(context, null, true);
 }

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
    if (view instanceof EditText) {
        EditText editText = (EditText) view;
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
    } else {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.edtCode.setFocusable(false);
        holder.edtCode.setFocusableInTouchMode(false);
    }
    return false;
 }

 private class ViewHolder {
    TextView txtName;
    EditText edtCode;
}

 @Override
 public View newView(final Context context, Cursor cursor, ViewGroup parent) {
    View convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
    final ViewHolder holder = new ViewHolder();
    holder.txtName = (TextView) convertView.findViewById(R.id.txt_name);
    holder.edtCode = (EditText) convertView.findViewById(R.id.pass);
    holder.edtCode.setOnTouchListener(this);
    convertView.setOnTouchListener(this);
    convertView.setTag(holder);

    return convertView;
 }

@Override
public void bindView(View view, Context context, Cursor cur) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (cur!=null) holder.txtName.setText(cur.getString(cur.getColumnIndex("name")));
 }
}

and of course: android:windowSoftInputMode="adjustPan" for activity in the manifest.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...