EditText Loses Focus on Click

2019-01-25 20:25发布

I'm writing an activity to list a bunch of items from the database and show an additional column with a quantity. I implemented an ArrayAdapter to show my custom layout. Here is the code of the most important parts of the Adapter:

static class ViewHolder {
    protected TextView text;
    protected EditText editText;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowquantitylayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        viewHolder.editText = (EditText) view.findViewById(R.id.editText);
        viewHolder.editText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "Editable:" + s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
            }
        });
        view.setTag(viewHolder);
        viewHolder.editText.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).editText.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getDescription());
    holder.editText.setText("" + list.get(position).getQuantity());
    return view;

}

Now, when I try to focus the "EditText" it loses focus just after I clicked it. It shows the keboard and it even gets to the Log.i, but doesnt let me change the value since it is not focused.

Thanks for the help

2条回答
聊天终结者
2楼-- · 2019-01-25 20:55

I had the same problem and I searched for an answer. Here is an answer to a similar question : https://stackoverflow.com/a/9338694/1448661

You have to put a line in your activity in the manifest :

    android:windowSoftInputMode="adjustPan"

And your listview must have this line too :

    android:descendantFocusability="beforeDescendants"

And now you will probably face another problem which is the lost of your text when scrolling. If this happens take a look at this tutorial : http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/

Hope this will help. Goui

查看更多
Root(大扎)
3楼-- · 2019-01-25 21:02

try this:

<EditText
    android:id="@+id/editText1"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:inputType="textShortMessage" >
    <requestFocus />
</EditText>
查看更多
登录 后发表回答