Android EditText 禁止换行

来源:互联网 发布:中国古代星象学 知乎 编辑:程序博客网 时间:2024/06/09 18:07

在做登录框的时候,很多时候要在输入框禁止换行输入,一般有两种方法:

第一种,就是监听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.     />  

感觉第二种方法更好一些
0 0