Android如何在ListView中使EditText可编辑

时间:2023-05-03
本文介绍了Android如何在ListView中使EditText可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 ListView,每个项目中都有编辑文本,我想知道如何使 EditText 可编辑.如果我设置android:windowSoftInputMode="adjustPan" 进入清单,但仅在某些设备上.在某些设备上,软键盘会覆盖编辑文本.

I have a ListView with edit text in each items and I want to know how can I make the EditText editable.. It seems to work fine if I set the android:windowSoftInputMode="adjustPan" into the manifest but only on some devices. On some devices the soft keyboard covers the edittext.

另一方面,如果我没有将 android:windowSoftInputMode="adjustPan" 放在清单中,则 EditText 在显示软键盘后失去焦点并且我必须多次触摸 EditText 才能将其选中并可以在其中写入.

On the other side, if I don't put android:windowSoftInputMode="adjustPan" in the manifest, the EditText looses focus after the soft keyboard is displayed and I have to touch the EditText multiple times to make it selected and can write in it.

请帮我解决这个问题.

注意:我看过这篇文章:ListView 内的可聚焦 EditText 没有任何帮助,除此之外我还有其他问题

Note: I've seen this post: Focusable EditText inside ListView and nothing helped from there and I have totally other problem than that

推荐答案

我为此做了一个解决方法..也许会帮助某人

I made a workaround for this though.. maybe will help someone

public class EditTextListAdapter extends BaseAdapter {

    /* notes list */
    private ArrayList<SomeData> mSomeData = null;
    /* layout inflater instance */
    private LayoutInflater mInflater;
    /* activity context */
    private Context mContext;

    /* ensure that this constant is greater than the maximum list size */
    private static final int DEFAULT_ID_VALUE = -1;
    /* used to keep the note edit text row id within the list */
    private int mNoteId = DEFAULT_ID_VALUE;

    /**
     * EditTextListAdapter adapter class constructor
     *
     * @param context               activity context
     */
    public FirstTimesListAdapter(Context context) {

        /* get a layout inflater instance */
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /* load the data */
        mSomeData = SomeData.instance().getData(); 
        /* keep the context */
        mContext = context;
    }

    /**
     * Returns the selected item
     *
     * @return selected item
     */
    public int getSelectedItem() {
        return mSelectedItem;
    }

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

    public Object getItem(int i) {
        return mSomeData.get(i);
    }

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

    public View getView(final int index, View recycledView, ViewGroup viewGroup) {

        ViewHolder viewHolder;

        if (recycledView == null) {
            /* inflate the list item */
            recycledView = mInflater.inflate(R.layout.listview_item_with_edit_text, viewGroup, false);
            /* get link to the view holder views */
            viewHolder = new ViewHolder();
            viewHolder.note = (EditText) recycledView.findViewById(R.id.first_times_note);
            recycledView.setTag(viewHolder);
        } else {
            /* reuse the same views we load the first time */
            viewHolder = (ViewHolder) recycledView.getTag();
        }

        /* display some notes */
        viewHolder.note.setText(mSomeData.getNotes());

        /* if the last id is set, the edit text from this list item was pressed */
        if (mNoteId == index) { 

            /* make the edit text recive focus */
            viewHolder.note.requestFocusFromTouch();
            /* make the edit text's cursor to appear at the end of the text */
            viewHolder.note.setSelection(viewHolder.note.getText().length());

            /* reset the last id to default value */
            mNoteId = DEFAULT_ID_VALUE;
        }

        /* set a touch listener on the edit text just to record the index of the edit text that was pressed */
        viewHolder.note.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    /* get the index of the touched list item */
                    mNoteId = index;
                }
                return false;
            }
        });

        return recycledView;
    }

    static class ViewHolder {        
        /* note input */
        EditText note;
    }

}

这篇关于Android如何在ListView中使EditText可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:无法更改 Xcode 4.5 中的目标成员身份可见性 下一篇:TextInputLayout 和 EditText 双提示问题

相关文章

最新文章