【问题标题】:Editext inside listview not taking focus and don't let me edit android列表视图中的Editext没有集中注意力,不要让我编辑android
【发布时间】:2012-12-21 06:35:28
【问题描述】:

编辑

现已解决

当我在 Adapter 类中删除 addTextChangeLister 时,editext 获得焦点,它让我可以编辑值,但是当用户输入时,我如何添加 addTextChangeListener 来计算报价的值。

我在列表项中有 2 个Editext(数量和价格)和一个TextView(报价)。我希望用户可以输入价格和数量,然后在用户更改两个值中的任何一个(数量或Edittext 中的价格)时计算报价。这是屏幕截图

但是 Listview 里面的 Editext 没有集中注意力,不要让我编辑它这是代码......

这是列表视图 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/ep_multiselect"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="3"
        android:hint="Enter Product Name"
        android:textSize="16sp" />

    <Button
        android:id="@+id/ep_go"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/ep_multiselect"
        android:text="   Go   " />

    <ListView
        android:id="@+id/ep_listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ep_go"
        android:focusable="false"
        android:visibility="gone" >
    </ListView>

</RelativeLayout>

这里是列表项代码...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/ep_productName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dip"
        android:layout_toLeftOf="@+id/ep_checkBox"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/ep_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ep_productName"
        android:layout_toLeftOf="@+id/ep_price"
        android:ems="10"
        android:focusable="true"
        android:hint="Qty"
        android:inputType="number" >
    </EditText>

    <EditText
        android:id="@+id/ep_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ep_qty"
        android:layout_alignBottom="@+id/ep_qty"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:focusable="true"
        android:hint="Price"
        android:inputType="number" />

    <TextView
        android:id="@+id/ep_quotePrice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/ep_qty"
        android:text="Quote Price"
        android:textSize="16sp" />



</RelativeLayout>

这就是我在 java 中的处理方式..

private class MyEditProductAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<ProductDetailBean> productDetails;

    private boolean listType;

    public MyEditProductAdapter(LayoutInflater arg1,
            ArrayList<ProductDetailBean > productDetails,
            boolean listType) {
        this.inflater = arg1;

        this.productDetails = productDetails;
        this.listType = listType;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return productQuantity.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return productQuantity;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (convertView == null) { 
            holder = new Holder();

            // inflate the view from xml
            convertView = inflater.inflate(R.layout.edit_product_listitems,
                    null);


            holder.productName = (TextView) convertView
                    .findViewById(R.id.ep_productName);
            holder.qty = (EditText) convertView.findViewById(R.id.ep_qty);
            holder.quotePrice = (TextView) convertView
                    .findViewById(R.id.ep_quotePrice);
            holder.price = (EditText) convertView
                    .findViewById(R.id.ep_price);

            holder.box = (CheckBox) convertView
                    .findViewById(R.id.ep_checkBox);

            holder.qty.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    if (arg0.length() > 0) {
                        holder.quotePrice.setText("Quote Price : "
                                + Integer.parseInt(arg0.toString())
                                * productPrice.get(position));
                        notifyDataSetChanged();
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }
            });

            holder.price.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    if (arg0.length() > 0) {
                        holder.quotePrice.setText("Quote Price : "
                                + Integer.parseInt(arg0.toString())
                                * productQuantity.get(position));
                        notifyDataSetChanged();
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }
            });

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final int localproductPrice = this.productPrice.get(position);
        final int localqty = this.productQuantity.get(position);
        holder.productName.setText(this.productName.get(position));
        holder.qty.setText("" + localqty);
        holder.price.setText("" + localproductPrice);
        holder.quotePrice.setText("Quote Price : "
                + (localqty * localproductPrice));

        return convertView;
    }
    }

    static class Holder {
        TextView productName, quotePrice;
        CheckBox box;
        EditText qty, price;
    }

【问题讨论】:

    标签: android android-listview focus android-edittext


    【解决方案1】:

    尝试添加

    android:descendantFocusability="blocksDescendants"

    在 list_item.xml 的父布局中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多