重点的EditText ListView中时块后代(机器人)重点的EditText ListView

2019-05-12 05:01发布

我有一个自定义的ListView。 每一行都有一个EditText, Buttons & TextView 。 为了使点击我一直ListView的项目android:descendantFocusability="blocksDescendants"为行布局。 如果我不守descendantFocusability我无法实现的动作onItemClick 。 如果我继续descendantFocusabilityEditText这是目前在我行未获得焦点。 我想EditText可获得焦点,也是我应该可以点击每行导航到另一个Activity 。 任何人都可以请帮我在这。 感谢所有。

编辑:在CustomAdapter的EditText上,我试图保持onTouchListeneronClickListener ,我requestFocus的,但似乎并没有工作。

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recentrowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/margin_left_5"
android:clickable="true"
tools:ignore="UseCompoundDrawables,HardcodedText,ContentDescription,UselessParent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edit_text_bg"
    android:padding="@dimen/margin_left_5" >

    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/addSubscribe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/flikart_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="FLIKART"
            android:textColor="@color/gray"
            android:textSize="@dimen/medium_text_size" />

        <ImageView
            android:id="@+id/addToFav"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/rl2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rl1"
        android:layout_marginLeft="@dimen/margin_30"
        android:layout_marginRight="@dimen/margin_30"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text_desciption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:lines="2"
            android:text="20% off on Smart Phones and basic Handsets has upto 50% OFF only"
            android:textColor="@color/blue" />

        <TextView
            android:id="@+id/text_desciption2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:lines="1"
            android:text="somethign something,......."
            android:textColor="@color/gray" />

        <TextView
            android:id="@+id/couponTypeText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_left_10"
            android:background="@drawable/button_bg"
            android:gravity="center"
            android:padding="@dimen/margin_left_10"
            android:text="STEAL THE DEAL"
            android:textColor="@color/white"
            android:textSize="@dimen/little_small_text_size" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rl3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rl2"
        android:layout_marginTop="@dimen/margin_left_10" >

        <TextView
            android:id="@+id/text_offer_expiry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/margin_left_5"
            android:text="Ends 10 days"
            android:textColor="@color/red"
            android:textSize="16sp" />

        <RelativeLayout
            android:id="@+id/rightLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp" >

            <ImageView
                android:id="@+id/comment_image_total"
                android:layout_width="@dimen/dimenstion_25"
                android:layout_height="@dimen/dimenstion_20"
                android:background="@drawable/comments" />

            <TextView
                android:id="@+id/text_comments"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/comment_image_total"
                android:text="100 Comments"
                android:textColor="@color/gray"
                android:textSize="16sp" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ll2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/rightLayout"
            android:layout_toRightOf="@id/text_offer_expiry"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/like_image_total"
                android:layout_width="@dimen/dimenstion_25"
                android:layout_height="@dimen/dimenstion_25"
                android:background="@drawable/like" />

            <TextView
                android:id="@+id/text_total_likes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/like_image_total"
                android:text="999 Likes"
                android:textColor="@color/gray"
                android:textSize="16sp" />
        </RelativeLayout>
    </RelativeLayout>

    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="4dp"
        android:layout_below="@id/rl3"
        android:layout_marginTop="@dimen/margin_5"
        android:background="@color/purple_clor" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/title_bar_height"
        android:layout_below="@id/view"
        android:layout_centerVertical="true"
        android:layout_marginTop="@dimen/margin_5" >

        <RelativeLayout
            android:id="@+id/likesLayout"
            android:layout_width="@dimen/dimenstion_40"
            android:layout_height="@dimen/dimenstion_40"
            android:layout_centerVertical="true"
            android:background="@drawable/unratedbkg" >

            <Button
                android:id="@+id/likesBtn"
                android:layout_width="@dimen/dimenstion_30"
                android:layout_height="@dimen/dimenstion_30"
                android:layout_centerInParent="true"
                android:background="@drawable/unrated" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/footermain"
            android:layout_width="fill_parent"
            android:layout_height="@dimen/dimenstion_40"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/likesLayout"
            android:background="@color/white" >

            <RelativeLayout
                android:id="@+id/animLayout"
                android:layout_width="100dp"
                android:layout_height="@dimen/dimenstion_40"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/likesLayout"
                android:background="@drawable/ratingbkg"
                android:visibility="gone" >

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_margin="@dimen/margin_5" >

                    <ImageView
                        android:id="@+id/like_image"
                        android:layout_width="@dimen/dimenstion_30"
                        android:layout_height="@dimen/dimenstion_30"
                        android:layout_centerInParent="true"
                        android:background="@drawable/like" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_margin="@dimen/margin_5" >

                    <ImageView
                        android:id="@+id/dislike_image"
                        android:layout_width="@dimen/dimenstion_30"
                        android:layout_height="@dimen/dimenstion_30"
                        android:layout_centerInParent="true"
                        android:background="@drawable/dislike" />
                </RelativeLayout>
            </RelativeLayout>

            <EditText
                android:id="@+id/add_comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/animLayout"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:hint="Add Comment"
                android:inputType="text" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

</LinearLayout>

Answer 1:

请不要使用setOnItemClickListener的项目点击..我认为你应该适配器方法中使用项目视图点击

convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "click item",Toast.LENGTH_LONG).show();
            }
        });

从主列表项的布局中删除此

android:descendantFocusability="blocksDescendants" 

感谢和享受这个代码!



Answer 2:

尝试这一行添加到您的活动在manifest.xml

android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"


Answer 3:

you do like this:

first set in your listview android:focusable="false";

那么你要被点击行:这个在您的customAdapterü应该做这样的

 v.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) { 
                // TODO Auto-generated method stub
            Toast.makeText(context, "Hello world",2000).show(); 

            }
        });

它将工作。 默认情况下,您的项目现在应该点击选项,当你让机器人:在列表视图可聚焦=“假”。 无需在列表视图中使用-descendantFocusability。



Answer 4:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView android:id="@+id/MyList" android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
    </ListView>
</LinearLayout>

item.xml

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

    <EditText android:id="@+id/ItemCaption"
        android:layout_height="wrap_content" android:layout_width="fill_parent"
        android:layout_marginLeft="2dip" android:singleLine="true">
    </EditText>

</LinearLayout>

AndroidCustomListViewActivity

public class AndroidCustomListViewActivity extends Activity {
    private ListView myList;
    private MyAdapter myAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myList = (ListView) findViewById(R.id.MyList);
        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();
        myList.setAdapter(myAdapter);

    }

    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        public ArrayList myItems = new ArrayList();

        public MyAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < 20; i++) {
                ListItem listItem = new ListItem();
                listItem.caption = "Caption" + i;
                myItems.add(listItem);
            }
            notifyDataSetChanged();
        }

        public int getCount() {
            return myItems.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item, null);
                holder.caption = (EditText) convertView
                        .findViewById(R.id.ItemCaption);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //Fill EditText with the value you have in data source
            holder.caption.setText(myItems.get(position).caption);
            holder.caption.setId(position);

            //we need to update adapter once we finish with editing
            holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        final int position = v.getId();
                        final EditText Caption = (EditText) v;
                        myItems.get(position).caption = Caption.getText().toString();
                    }
                }
            });

            return convertView;
        }
    }

    class ViewHolder {
        EditText caption;
    }

    class ListItem {
        String caption;
    }
}


Answer 5:

main.xml中

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true" >
</ListView>

创建ArrayList中添加数据到ArrayList中。

lv=(ListView)findViewById(R.id.listView1);
lv.setItemsCanFocus(true);

for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i);
}

1)创建为ListView适配器,并设置位置标记为的EditText。

2)一般情况下,滚动条位置时将change.So,你必须得到的EditText标签,并将其设置到EditText上id.from你能够避免物品位置的变化。

holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position); 
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);

}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position); 

最后,文观察者添加到的EditText和存储变为列表中的正确位置。

holder.caption.addTextChangedListener(new TextWatcher() {

             @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                      final int position2 = holder.caption.getId();
                      final EditText Caption = (EditText) holder.caption;
                      if(Caption.getText().toString().length()>0){
                       list.set(position2,Integer.parseInt(Caption.getText().toString()));
                      }else{
                       Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
                      }

                  }

             @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
            }

             @Override
            public void afterTextChanged(Editable s) {

            }

         });

请参阅此链接.. http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html



文章来源: Focus on EditText in ListView when block descendants (Android)