Android EditText in ListView - keyboard

2019-02-04 11:33发布

问题:

I've got a problem with my EditText. I use the following adapter:

public class RowTextViewAdapter extends BaseAdapter {

...

  public View getView(int position, View convertView, ViewGroup parent) {

  ViewHolder holder = null;

    if (rowTitles.get(position).equals("edit")) {
        if(et == null){
            et = new EditText(activity);
            et.setText("Test");
        }
        return et;
    }
    else {
        convertView = new TextRow(activity);
        holder = new ViewHolder(((TextRow) convertView).getTextView(), ((TextRow) convertView).getImageView());
        convertView.setTag(holder);
        holder.getTextView().setText(StringManager.getInstance().getText(rowTitles.get(position), activity));
        holder.getImageView().setImageBitmap(assetController.getBitmap(additiveIcons.get(position) + ".png", null));
        return convertView;
    }
  }
}

and ListActivity:

public class AppSettingActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new RowTextViewAdapter(this);
        adapter.addRowView("account", "arrowDw");
        adapter.addRowView("password", "arrowDw");
        setListAdapter(adapter);
   }

...

   protected void onListItemClick(ListView listView, View view, int position, long id) {
    switch (position) {
        case 0: accIsEditable = adapter.setEditable(position); break;
        case 1: 
            if(accIsEditable) {
                                    //TODO do something..
                break;
            }
            pwIsEditable = adapter.setEditable(position);
            break;
          ...
   }
}

If i click on the first item I add a new list item on pos. 1 (pos: 0, 1, 2, ...). Now the EditText field is added to the list.

ListView:
----------------------------          -------------------------    
Account                   v           Account                ^     
----------------------------    ==>   -------------------------     
Passowrd                  v           [::::::::EditText:::::::]      
----------------------------          -------------------------
//more..                              Password               v
----------------------------          -------------------------
                                      //more..
                                      -------------------------

If I click now into the EditText field, it shows the virtual keyboard and loses focus of the EditText. I click again and it gains focus. But if I write something, the text is only showed in the EditText field, if i tap on it and not frequently while i'm writing...

Any idea to fix that update problem?

回答1:

If this is still an issue, take a look at this item: Focusable EditText inside ListView

This changes may help.

Change to listview:

<ListView
    android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:descendantFocusability="beforeDescendants"
    />

Change to activity in mainfest.xml:

<activity android:name= ".yourActivity" android:windowSoftInputMode="adjustPan"/>


回答2:

some times when you use android:windowSoftInputMode="stateAlwaysHidden"in manifest activity or xml, that time it will lose keyboard focus. So first check for that property in your xml and manifest,if it is there just remove it. After add these option to manifest file in side activity android:windowSoftInputMode="adjustPan"and add this property to listview in xml android:descendantFocusability="beforeDescendants"