Focus on EditText in ListView when block descendants (Android)

后端 未结 5 1680
一整个雨季
一整个雨季 2020-12-01 21:36

I have a customized ListView. Each row has an EditText, Buttons & TextView. In order to make the ListView items clickable I have kept

相关标签:
5条回答
  • 2020-12-01 22:17

    Try to add this line to your activity in the manifest.xml

    android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
    
    0 讨论(0)
  • 2020-12-01 22:21

    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>
    

    Create ArrayList add data into the 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)create adapter for the listview and set the position as tag for the edittext.

    2)Normally,when scrolling the item position will change.So,you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.

    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); 
    

    Finally,add the text watcher to the edittext and store the changes into correct position in the list.

    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) {
    
                }
    
             });
    

    Refer this link..http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html

    0 讨论(0)
  • 2020-12-01 22:22

    you do like this:

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

    then you want row to be clicked: for this in your customAdapter you should do like this

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

    It will work.

    By default, your items should now have the click option, when you made android:focusable="false" in listview.

    No need to use descendantFocusability in listview.

    0 讨论(0)
  • 2020-12-01 22:27

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 22:30

    please Don't use setOnItemClickListener for item click .. i think that you should be use item view click inside adapter method

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

    Remove this from main list item layout

    android:descendantFocusability="blocksDescendants" 
    

    Thanks and enjoy this code !

    0 讨论(0)
提交回复
热议问题