EditText Loses Focus on Click

☆樱花仙子☆ 提交于 2019-12-29 07:06:21

问题


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


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/14910131/edittext-loses-focus-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!