在 ListView (Android) 中聚焦 EditTexts 的问题 [英] Issues focusing EditTexts in a ListView (Android)
问题描述
我有一个 ListView,每个项目中有几个 EditText.我对硬件键盘没有任何问题,但软键盘有点吓人.我有两个问题.
I have a ListView with a few EditTexts in each item. I have no issues with a hardware keyboard, but things freak out a bit with the soft keyboard. I have two issues.
- 当我第一次点击 EditText 时,它会短暂地看起来有焦点,但一旦键盘显示,它就会失去焦点.然后我必须再次单击 EditText 才能获得焦点.
- 当带有焦点的 EditText 被滚动到视图之外时,焦点会转到......好吧......看截图.我不确定发生了什么.
#1 的更多细节:
- 当屏幕第一次加载时,焦点位于Gr High School Scale"字段中,但未显示键盘.
- 如果我立即单击所需的 EditText,它的
OnFocusChangeListener
会告诉我它获得焦点然后失去焦点.在视觉上,我看到光标出现在字段中,但是当键盘加载时,光标跳开(如屏幕截图所示),我不知道焦点去了哪里.
- When the screen first loads, focus is in the "Gr High School Scale" field, but the keyboard is not shown.
- If I immediately click on a desired EditText, its
OnFocusChangeListener
tells me that it receives focus and then loses focus. Visually, I see the cursor appear in the field, but when the keyboard loads the cursor jumps away (like in the screenshot) and I don't know where focus has gone.
我已经尝试了 ListView 的 Focusable 和 DescendantFocusability 属性,但无济于事.有什么建议吗?
I've played around a bit the Focusable and DescendantFocusability attributes of the ListView, but to no avail. Any advice?
每次有焦点的 EditText 滚出视图时,都会出现另一个醉酒光标:
Each time an EditText with focus gets scrolled out of view, another drunk cursor shows up:
更新:相关代码.
带有 ListView XML 的活动布局:
UPDATE: Relevant code.
Activity layout with ListView XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.NsouthProductions.gradetrackerpro"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.NsouthProductions.gradetrackerpro.Activity_EditCourseGPA" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Define the GPA and Percent scale for this course." />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="5dp" >
<LinearLayout
android:id="@+id/linlay_rad_group_existing_scale"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/radio0_existing_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:checked="true"
android:text="Existing Scale" />
<Spinner
android:id="@+id/spinner_existing_scales"
android:layout_width="wrap_content"
android:layout_height="33dp"
android:layout_gravity="right"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/linlay_rad_group_new_scale"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1_new_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="New Scale" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:text=" " />
<EditText
android:id="@+id/editText1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPersonName"
android:maxLines="1"
android:scrollHorizontally="true"
android:text="Gr High School Scale" >
<requestFocus />
</EditText>
</LinearLayout>
</RadioGroup>
<LinearLayout
android:id="@+id/linlay_scale_save_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
<Button
android:id="@+id/btn_gpa_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/btn_gpa_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<ListView
android:id="@+id/listview_scale"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/linlay_scale_save_buttons"
android:layout_alignParentLeft="true"
android:layout_below="@id/radioGroup1"
android:focusable="true"
android:focusableInTouchMode="true"
android:headerDividersEnabled="true"
android:scrollingCache="true" >
</ListView>
列表项 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linlay_scale_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkbox_scale_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A-" />
<EditText
android:id="@+id/et_gpa"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="true"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="4.000" />
<EditText
android:id="@+id/et_min"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="true"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="94.75" />
<EditText
android:id="@+id/et_max"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:maxLength="6"
android:text="100.0" />
在我的适配器中设置视图:
Setting the view in my adapter:
View v = convertView;
ViewHolder holder;
v = inflater.inflate(R.layout.scale_list_item,
holder = new ViewHolder();
holder.cb = (CheckBox) v.findViewById(R.id.checkbox_scale_item);
holder.et_gpa = (EditText) v.findViewById(R.id.et_gpa);
holder.et_min = (EditText) v.findViewById(R.id.et_min);
holder.et_max = (EditText) v.findViewById(R.id.et_max);
我不知道还有什么要发布的.我有 focus 和 textChange 侦听器,但是即使将它们注释掉,问题也存在.让我知道是否需要其他任何东西.谢谢.
I'm not sure what else to post. I have focus and textChange listeners, but the problem exists even if those are commented out. Let me know if anything else is needed. Thank you.
有关触摸 EditText 时焦点行为的更多详细信息:
More detail about how focus is behaving when the EditText is touched:
- 点击(触摸)EditText
- EditText 接收焦点
- EditText 失去焦点
- ListView 获得焦点(并尝试设置子焦点视图
requestChildFocus
... 似乎没有成功). - ListView 失去焦点
- ListView 获得焦点(并再次尝试设置子焦点).
- The EditText is clicked (touched)
- EditText Receives focus
- EditText loses focus
- ListView gains focus (and tries to set child focus view
requestChildFocus
... doesn't seem to succeed). - ListView loses focus
- ListView gains focus (and tries to set child focus again).
以上基于对 EditText 和 ListView 都有侦听器.注意: 使用硬件键盘,EditText 获得焦点,仅此而已.我认为出现的软键盘会影响焦点.
The above is based on having listeners for both the EditText and the ListView. Note: with a hardware keyboard, the EditText gets focus and that's that. I think the soft keyboard appearing affects the focus.
最终更新: 最后,使用 ListView 太困难了,尤其是因为我想根据对一行中 EditText 的更改来更新多行.当键盘打开并且 ListView 在任何时候只考虑存在的几行时,这很难做到.
FINAL UPDATE: In the end, working with the ListView was too difficult, especially because I wanted to update multiple rows based on changes to an EditText in one row. That's hard to do when the keyboard is up and the ListView only considers a few of the rows to exist at any one time.
我改为使用嵌套的 LinearLayouts.每个水平布局都是一个行",我将它们放在一个 ArrayList 中以便管理它们,相对来说这是小菜一碟(仍然不容易).
I instead made nested LinearLayouts. Each horizontal layout was a "row" and I put them inside of an ArrayList in order to manage them and it was a piece of cake, comparatively (still not easy).
推荐答案
您正面临 ListView
回收问题.当您向上或向下滚动或出现键盘时 ListView
再次刷新并失去 EditText 的
焦点.所以,首先阅读这个答案以了解ListView
回收机制,然后按照来自我的身边是我脑海中你当前场景的解决方案.
You are facing ListView
recycling issue. When you scroll up or down or when keyboard appears ListView
again refreshes and lost your EditText's
focus. So, first of all read this answer to understand ListView
Recycling mechanism and then follow the suggestion from my side the solution of your current scenario in my mind.
我建议你应该在 ListView
项目和文本上使用 Button
这个按钮应该是通用的,比如输入学生信息或你想要的任何东西.单击此 Button
后,打开 AlertDialog
并设置您的 xml 视图(当前您所有的 edittexts
,如 listview 上的 et_gpa、et_min、et_max 等
项)
I suggest you should use a Button
on ListView
Item and text of this button should be generic like Enter Student Info or what ever you'd like. After clicking on this Button
open AlertDialog
and set your xml view (currently your all edittexts
like et_gpa, et_min, et_max etc on listview
items)
例如:
btnInfo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showStudentInfoAlert();
}
});
public void showStudentInfoAlert()
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater in = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = in.inflate(R.layout.your_xml_having_edittexts, null);
EditText et_gpa = (EditText) v.findViewById(R.id.et_gpa);
//add all edit texts like this and after that just set view on alert dialog
builder.setTitle("Enter student's info");
builder.setView(v);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//save all edittexts values and do what erver you want with those values
}
});
dialog.show();
}
这篇关于在 ListView (Android) 中聚焦 EditTexts 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!