聚焦EditTexts在ListView问题(安卓) [英] Issues focusing EditTexts in a ListView (Android)

查看:151
本文介绍了聚焦EditTexts在ListView问题(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个EditTexts中的每一项ListView控件。我有一个硬件键盘没有任何问题,但事情吓坏了,未免有软键盘。我有两个问题。

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.

  1. 当我第一次点击一个EditText,简单的说似乎有焦点,但随后失去焦点,一旦键盘显示。然后,我必须再次点击的EditText获得焦点。
  2. 在当前具有焦点一个EditText被滚出来看,重点去...好...见截图。我不知道发生了什么。

在#1的更多细节:

  • 当屏幕第一次加载,重点是在的Gr高中规模领域​​,但键盘不显示。
  • 如果我马上单击所需的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控件的可调焦和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:

更新:相关code
。 与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);

我不知道张贴什么。我有重点和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:

  1. 的EditText上点击(触摸)
  2. 的EditText接收焦点
  3. 的EditText失去焦点
  4. 的ListView获得焦点(并尝试设置子焦点视图 requestChildFocus ......似乎并没有成功)。
  5. 在ListView控件失去焦点
  6. 的ListView获得焦点(并尝试设置子焦点再次)。
  1. The EditText is clicked (touched)
  2. EditText Receives focus
  3. EditText loses focus
  4. ListView gains focus (and tries to set child focus view requestChildFocus... doesn't seem to succeed).
  5. ListView loses focus
  6. 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 项目这个按钮和文本应该是通用的像进入学生信息或者你什么都想要。点击这个按钮后打开 AlertDialog 和设置您的XML视图(目前你所有的 edittexts 像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();
    }

这篇关于聚焦EditTexts在ListView问题(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆