Android EditText 禁止换行

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

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

	/**
	 * 设置相关监听器
	 */
	private void setListener(){
		userNameEdit.setOnEditorActionListener(new OnEditorActionListener() {
			@Override
			public 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"
        />

感觉第二种方法更好一些

猜你喜欢

转载自blog.csdn.net/howlaa/article/details/18596063