1
先上佈局一個EditText添加imeOptions監聽回車事件
<EditText
android:id="@+id/et_release_experience_input_tag"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:layout_gravity="center_horizontal"
android:hint="添加標籤"
android:maxLines="1"
android:textColorHint="@color/gray"
android:textCursorDrawable="@null" />
2
setOnEditorActionListener實現此回調事件,當事件時event.getKeyCode()爲KeyEvent.KEYCODE_ENTER時爲回車事件,返回true,表示我們處理此事件,這樣回車就不會換行了。
etInputTag.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//回車時返回true攔截事件,不讓換行
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (TextUtils.isEmpty(etInputTag.getText().toString().trim())) {
CommonUtils.showToast(ReleaseExperienceActivity.this, "請先輸入標籤!");
} else {//不爲空時才添加標籤
tagLists.add(etInputTag.getText().toString());
mAdapter.clearAndAddAll(tagLists);
etInputTag.setText("");
}
return true;
}
return false;
}
});