35

ListView在一个单独的 XML 文件中定义了每个项目的布局。在这个文件中,我包含了一个RatingBar和一个EditText.

我以编程方式创建了 7-8 个项目ListView。当我滚动浏览它们时,它似乎很麻烦。这里有些例子:

  1. 如果我将焦点设置为EditText第一行中的 ,然后向下滚动ListView,其他行中的随机EditTexts将具有焦点。似乎是EditText焦点消失后的下一个接收焦点。也许这是故意的,但是,作为用户,这似乎很奇怪。

  2. 如果我将焦点设置为EditText,接收一个虚拟键盘,输入一些内容,然后单击虚拟键盘上的“完成”按钮,EditText虚拟键盘消失后就会清空。

  3. 有时,当我单击一个EditText,收到一个虚拟键盘并开始输入字母时,这些字母会在我输入后立即消失。

  4. 当我单击 时EditText,虚拟键盘会显示出来,但EditText失去焦点,我必须EditText再次单击。

  5. 即使我已将 设置RatingBarfocusable="false",如果我移动滚轮,它仍然会抓住焦点。

我的一个问题是,当我在虚拟键盘中键入一个字符时,所有可见的列表项都会被重绘(并且由于文本EditText设置为一些数据,它是空的,它被清除了。我不明白为什么 Android 会决定每次我输入一个字符时重新绘制列表。

这是我用来绘制它们的 XML。它们是白色的气泡,有一个灰色的边框,还有一些文字,一个RatingBar和一个EditText里面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="2dip"
        android:background="@drawable/shape_outer">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="2dip"
            android:background="@drawable/shape_inner">
                    <TextView
                            android:id="@+id/rating_category"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/dark_gray"
                            android:textStyle="bold"
                            android:layout_marginBottom="10dip" />
                        <RatingBar 
                            android:id="@+id/rating_rating"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:numStars="5"
                            android:rating="0"
                            android:stepSize="1"
                            android:focusable="false"
                            android:clickable="false"
                            />
                        <EditText 
                            android:id="@+id/rating_text"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_column="1"
                            android:padding="6dip"
                            android:textColor="#000000"
                            android:gravity="left|top"
                            android:lines="3"
                            android:hint="Comment"
                            android:imeOptions="actionDone" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
4

5 回答 5

19

听起来 ListViews 不能很好地处理 EditTexts。我做了一些研究,共识似乎是“不要那样做”。所以我所采取的是创建一个简单的布局文件,它是一个内部带有 LinearLayout 的 ScrollView。在我的 onCreate 方法中,我为列表项使用的 View 膨胀并将其添加到 LinearLayout。我还将视图添加到 ArrayList,以便以后可以将数据保存在每个视图中。

这听起来合理吗?

于 2010-08-12T16:29:04.060 回答
5

好吧,将此添加到清单中的活动中...

android:windowSoftInputMode="adjustPan"

例子...

<activity android:name=".mapview.AddParkingLotActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"/>
于 2012-06-29T06:29:36.353 回答
4

我遇到了同样的问题。当键盘出现时,我在 LisView 中的 EditText 失去焦点,所以从我放置的 ListView 项目中获取 getView

final EditText editInput = (EditText)convertView.findViewById(R.id.edit_input);
editInput.requestFocusFromTouch();

这对我有用。

于 2011-12-08T13:17:14.207 回答
3

这两个步骤解决了我遇到的类似问题(ListView 中一行上的 EditText + 有趣的键盘行为)。

  1. android:windowSoftInputMode="adjustPan"属性添加到显示 ListView 的活动的 AndroidManifest.xml 文件中。
  2. android:descendantFocusability="afterDescendants"属性添加到 ListView 本身。

这是实际示例:

AndroidManifest.xml

    <activity
        android:name=".SubscriptionListActivity"
        android:windowSoftInputMode="adjustPan"
        android:label="@string/title_activity_subscriptions" >
    </activity>

布局.xml 文件

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="afterDescendants"
        android:id="@+id/subscriptionList" />

我在三星 Galaxy S3 上对其进行了测试,它适用于三星以及 SwiftKey 键盘。

于 2015-02-17T14:30:53.723 回答
0

我有一些问题我解决了只是评论这一行

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
于 2014-08-04T07:21:02.030 回答