[英]Handle Enter button soft keyboard
我正在嘗試處理Android軟鍵盤上的Enter
按鈕。 我也嘗試了StackOverflow在這篇文章中所說的,但是沒有運氣。
我有兩個EditText
字段:一個詢問用戶名,另一個詢問用戶電子郵件。 完成用戶名后,我想執行Next
操作,然后轉到下一個EditText
。
這是我的.xml文件:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/text_user_name" android:layout_marginTop="20dp"
android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
android:layout_weight="1"
android:imeOptions="actionNext"
android:inputType="text"
android:nextFocusDown="@+id/text_user_email"
android:hint="name"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@id/text_user_email"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:imeOptions="actionDone"
android:hint="***@email.com"/>
這是我的OnEditorActionListener
:
return new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
// Check user information
if (CheckUserInfo()) finished = true;
}
return true;
}
else
if (actionId == EditorInfo.IME_ACTION_NEXT && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
Toast.makeText(getApplicationContext(), "go a line down", Toast.LENGTH_SHORT).show();
return true;
}
else
{
return false;
}
}
};
我也嘗試更改Enter
按鈕的可見文本,以使第一個EditText
看起來像Next
,而最終的EditText
看起來像是Done
:
textUserName.setImeActionLabel("Next", KeyEvent.KEYCODE_ENTER);
....
textUserEmail.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER);
但是這部分也不起作用。
有人可以幫助我了解為什么我什么都做不了嗎? :_( 謝謝!
您可以使用
android:imeActionLabel="your text here"
android:imeOptions="actionNext"
對於下一個
和
android:imeActionLabel="your text here"
android:imeOptions="actionSend"
對於完成按鈕
你也可以這樣
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == KeyEvent.KEYCODE_ENTER) {
doSomeThing();
}
return false;
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.