【问题标题】:Limit listview, and display letters only限制列表视图,仅显示字母
【发布时间】:2014-10-10 22:10:21
【问题描述】:

我有一个向用户显示的列表视图,我正在想办法使列表视图成为非无限的,并将其限制为 25 个项目。我也在尝试限制用户在 EditText 中显示数字。我尝试在列表视图中添加 android:inputType="text|textNoSuggestions" ,但这已被证明是无效的。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/white_wallpaper"
                >

    <ListView
        android:id="@+id/listMessages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/divider"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:inputType="text|textNoSuggestions"
        android:padding="0dip"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/message_left" />

    <RelativeLayout 
        android:id="@+id/divider"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@color/off_white"
        android:layout_above="@+id/relSendMessage" />

    <RelativeLayout
            android:id="@+id/relSendMessage"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:background="#ddd"
            android:paddingLeft="10dp"
            android:layout_alignParentBottom="true">

        <EditText
            android:id="@+id/messageBodyField"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignBottom="@+id/sendButton"
            android:layout_alignTop="@+id/sendButton"
            android:layout_marginBottom="-4dp"
            android:layout_marginRight="10dp"
            android:inputType="text|textNoSuggestions"
            android:layout_toLeftOf="@+id/sendButton"
            android:background="@android:color/white"
            android:hint="@string/message_elipses"
            android:textColor="#000000"
            android:textColorLink="#adefda"
            android:textSize="14sp" />

        <Button
                android:id="@+id/sendButton"
                android:layout_width="72dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_margin="4dp"
                android:background="@drawable/button_send" />
    </RelativeLayout>

    <Button
        android:id="@+id/iSchedule"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:alpha="0.7"
        android:background="#C11B17"
        android:text="Schedule"
        android:textColor="#f2f2f2"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="serif" />

</RelativeLayout>

任何建议将不胜感激。

更新:

至于限制列表视图项目,我有以下代码: 以下是以编程方式显示列表视图的方式:

 messagesList = (ListView) findViewById(R.id.listMessages);
            messageAdapter = new MessageAdapter(this);

以下是否可行:

    @Override
    public int getCount() {
messagesList.setAdapter(messageAdapter);
        return 25;
    }

【问题讨论】:

  • 通过“试图限制用户在 EditText 中显示数字”,您的意思是您不希望用户能够在该字段中输入数字吗?如果你能清除它会有所帮助。
  • 啊,是的,这就是我的意思。我不希望用户能够在字段中输入数字

标签: java android android-layout android-activity android-listview


【解决方案1】:

所以要处理你的编辑文本问题,你可以使用这个

<EditText
    ...
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>

现在上面的字符是唯一可以输入到EditText中的字符了。您也可以在android:inputType 中更改键盘类型。 此外,您想限制 ListView 中的条目数。在代码的 BaseAdapter 方法中,更改:

@Override
public int getCount() {
    return 25;
}

更新:

你可以做的是创建一个自定义ArrayAdapter 并在那里修改 getCount 方法。由于getCount() 完全属于不同的类,您只想更改那里的最大值 25,然后将列表视图设置为该适配器。您可能想看看 here 了解更多 ArrayAdapters 的用法。

【讨论】:

  • 感谢您的及时更新。我已经解决了edittext的第一个问题。就列表视图而言,我在最初的帖子下添加了一个 uodate
  • 任何帮助将不胜感激
  • 我还注意到第一个 EditText 标准,它不允许间距。它成功地解决了我没有数字的问题,但由于某种原因,它不包括间距。你会碰巧有一个修复吗?非常感谢
  • 您的帮助是巨大的,我非常感谢您的支持。我只是在解决列表中项目的限制时遇到问题,因为这就是我声明列表的方式 - messagesList = (ListView) findViewById(R.id.listMessages); messageAdapter = new MessageAdapter(this);\
猜你喜欢
  • 2013-08-23
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多