Android EditText prohibits line breaks

When making a login box, it is often necessary to prohibit newline input in the input box. Generally, there are two methods:

The first one is to monitor the setOnEditorActionListener method of EditText, and then disable the enter key. The disadvantage of this method is that the enter key will still be displayed in the virtual keyboard:

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

The second method is to directly disable the enter key on the virtual keyboard by configuring android:singleLine="true" in the xml file of EditText, and it will not be displayed.

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

I feel that the second method is better

Guess you like

Origin blog.csdn.net/howlaa/article/details/18596063