Android how to make EditText editable inside a ListView(Android如何在ListView中使EditText可编辑)
问题描述
我有一个 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可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android如何在ListView中使EditText可编辑
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01