如何编辑EditText

时间:2017-02-12 12:50:53

标签: android listview android-edittext baseadapter

我有一个listview包含TextView,EditText和Checkbox。列表视图中每个项目的布局如下所示。我现在的问题是我不能 编辑editText,当我触摸editText进行编辑时,光标会跳开,我无法编辑editText中的值。

解决这个问题,我提到了一些帖子,我在EditText中添加了以下两行

android:focusable="true"
android:focusableInTouchMode="true"

但它没有解决问题

请同时注意下面发布的getView()方法。

请让我知道如何解决此问题以编辑EditText

项目布局

<?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:orientation="horizontal"
android:weightSum="3">

<TextView
    android:id="@+id/tv"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<EditText
    android:id="@+id/et"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:cursorVisible="true"
    android:focusable="true"
    android:focusableInTouchMode="true"/>

<CheckBox
    android:id="@+id/cb"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

getView

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View view = null;

    if (convertView == null) {
        LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = layoutinflater.inflate(R.layout.model, null);

        final ViewHolder holder = new ViewHolder();

        holder.tv = (TextView) view.findViewById(R.id.tv);
        holder.cb = (CheckBox) view.findViewById(R.id.cb);
        holder.et = (EditText) view.findViewById(R.id.et);

        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                ItemDesign element = (ItemDesign) holder.cb.getTag();
                element.setChecked(buttonView.isChecked());
            }
        });

        holder.cb.setTag(designList.get(position));
        holder.et.setTag(designList.get(position));
        holder.tv.setTag(designList.get(position));
        //holder.et.setFocusable(false);
        view.setTag(holder);
    } else {
        view = convertView;
        ((ViewHolder)view.getTag()).et.setTag(designList.get(position));
        ((ViewHolder)view.getTag()).tv.setTag(designList.get(position));
        ((ViewHolder)view.getTag()).cb.setTag(designList.get(position));
        //((ViewHolder)view.getTag()).et.setFocusable(false);
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.tv.setText(designList.get(position).getTxt());
    holder.cb.setChecked(designList.get(position).isChecked());
    holder.et.setText(designList.get(position).getEtTxt());
    //holder.et.setFocusable(false);
    return view;
}
private class ViewHolder {
    TextView tv;
    CheckBox cb;
    EditText et;
}

1 个答案:

答案 0 :(得分:1)

我认为您的问题与您添加的这三行代码无关

android:cursorVisible="true"
android:focusable="true"
android:focusableInTouchMode="true"

你可以删除它们。问题在于这条线。你将EditText宽度设置为零。

android:layout_width="0dp"

改变它,你很高兴

android:layout_width="wrap_content"