Android EditText 禁止換行

在做登錄框的時候,很多時候要在輸入框禁止換行輸入,一般有兩種方法:

第一種,就是監聽EditText的setOnEditorActionListener方法,然後把enter鍵禁止,這種方法有個不好的地方就是,在虛擬鍵盤中依然會顯示enter鍵:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. /** 
  2.  * 設置相關監聽器 
  3.  */  
  4. private void setListener(){  
  5.     userNameEdit.setOnEditorActionListener(new OnEditorActionListener() {  
  6.         @Override  
  7.         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  8.             return (event.getKeyCode()==KeyEvent.KEYCODE_ENTER);  
  9.         }  
  10.     });  
  11.       
  12.       
  13. }  

第二種方法是直接在EditText的xml文件中通過配置android:singleLine="true"把虛擬鍵盤上的enter鍵禁止掉,不會顯示。

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <EditText  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="38dp"  
  4.     android:id="@+id/loginUserNameEdit"  
  5.     android:background="@android:color/white"  
  6.     android:hint="登錄賬戶"  
  7.     android:paddingLeft="10dp"  
  8.     android:maxLines="1"  
  9.     android:singleLine="true"  
  10.     />  

感覺第二種方法更好一些
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章