1

我有一個列表視圖顯示給用戶,我想辦法使列表視圖非無限,並將其限制爲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> 

任何建議將不勝感激。

更新:

至於限制ListView的項目,我有下面的代碼: 下面是列表視圖是如何編程顯示:

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

將以下工作:

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

通過「試圖限制用戶無法在EditText中顯示數字」,您的意思是您不希望用戶能夠在該字段中輸入數字嗎?如果你能清楚這一點,這將有所幫助。 – apoorvk 2014-10-10 22:22:31

+0

啊,是的,這就是我的意思。我不希望用戶能夠在 – John 2014-10-10 22:36:39

回答

1

因此,要處理您的編輯文本問題,您可以使用此

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

現在上述字符是唯一可以輸入到EditText中的字符。您還可以更改android:inputType中的鍵盤類型。 此外,你想限制一個ListView中的條目數。在你的代碼的BaseAdapter方法,改變這種:

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

UPDATE:

那麼你可以做的是創建一個自定義ArrayAdapter和修改getCount將方法那裏。由於getCount()完全不在同一個類中,因此您只需要在那裏更改最大值,然後將列表視圖設置爲該適配器。您可能需要查看here以瞭解ArrayAdapter的更多用途。

+0

字段中輸入數字,感謝您的及時更新。我已經用edittext解決了第一個問題。至於列表視圖,我已添加一個uodate在我的初始職位 – John 2014-10-10 23:34:07

+0

任何協助將不勝感激 – John 2014-10-11 00:35:56

+0

我也注意到第一個EditText標準,它不允許間距。它已成功解決了我的問題,沒有數字,但由於某種原因它不包括間距。你偶然會有解決辦法嗎?非常感謝你 – John 2014-10-11 01:33:08