[英]Perform action on EditText enter pressed in android while retaining the enter icon on keyboard?
我想在 android editText 中按下 Enter 時執行一個操作。 我嘗試了各種使用EditorInfo.IME_ACTION_DONE
,這很有效,但問題是,它顯示了一個我不想要的勾號圖標。 我希望它看起來像鍵盤上的默認輸入按鈕。
如何在保留“輸入”形狀的同時處理輸入按下事件? 有沒有像EditorInfo.IME_ACTION_ENTER
這樣的東西來完成我的工作?
我的方法(顯示勾號圖標):
editorEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if( (actionId == EditorInfo.IME_ACTION_DONE){
Toast.makeText(EditorActivity.this, "working", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
我希望它看起來像“輸入”形狀,因為我正在制作一個文本編輯器。 我試圖在按下“Enter”按鈕時切換到另一個EditText
並且不希望它看起來像“Tick”按鈕。 這就是為什么我不想要刻度符號。
我認為只需在布局中更改EditText
的inputType
就可以在 Android 的軟鍵盤中使用類似“Enter”的按鈕。
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine" />
請注意,我使用textMultiLine
作為EditText
的inputType
。
您可以在EditText
按下“Enter”按鈕來執行一些操作,如下所示。
final EditText edittext = (EditText) findViewById(R.id.edit);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (before == 0 && count == 1 && s.charAt(start) == '\n') {
String text = edittext.getText().replace(start, start + 1, "").toString(); // Removes the enter
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
希望回答完畢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.