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.     />  

感觉第二种方法更好一些
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值