Android EditText 禁止换行与换行

来源:互联网 发布:qq酷双项淘宝客登陆 编辑:程序博客网 时间:2024/05/20 15:42

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

第一种,就是监听EditText的setOnEditorActionListener方法,然后把enter键禁止,这种方法有个不好的地方就是,在虚拟键盘中依然会显示enter键:

/** * 设置相关监听器 */private void setListener(){userNameEdit.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {return (event.getKeyCode()==KeyEvent.KEYCODE_ENTER);}});}

第二种方法是直接在EditText的xml文件中通过配置android:singleLine="true"把虚拟键盘上的enter键禁止掉,不会显示。     
    <EditText        android:layout_width="fill_parent"        android:layout_height="38dp"        android:id="@+id/loginUserNameEdit"      android:background="@android:color/white"      android:hint="登录账户"      android:paddingLeft="10dp"      android:maxLines="1"      android:singleLine="true"        />
惜绮梦手机精品饰品店 
0 0
原创粉丝点击