問題描述
當我按下下一步"時,用戶 EditText 上的焦點必須移至密碼.然后,從密碼,它必須向右移動,依此類推.你能幫我寫代碼嗎?
When I press the 'Next', the focus on the User EditText must be move to the Password. Then, from Password, it must move to the right and so on. Can you help me on how to code it?
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name*" />
<EditText
android:id="@+id/txt_User"
android:layout_width="290dp"
android:layout_height="33dp"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password*" />
<EditText
android:id="@+id/txt_Password"
android:layout_width="290dp"
android:layout_height="33dp"
android:singleLine="true"
android:password="true" />
<TextView
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password*" />
<EditText
android:id="@+id/txt_Confirm"
android:layout_width="290dp"
android:layout_height="33dp"
android:singleLine="true"
android:password="true" />
</LinearLayout>
推薦答案
焦點處理
焦點移動基于找到最近的算法給定方向的鄰居.在極少數情況下,默認算法可能與開發人員的預期行為不匹配.
Focus Handling
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer.
使用以下 XML 屬性更改定向導航的默認行為:
Change default behaviour of directional navigation by using following XML attributes:
android:nextFocusDown="@+id/.."
android:nextFocusLeft="@+id/.."
android:nextFocusRight="@+id/.."
android:nextFocusUp="@+id/.."
除了定向導航,您還可以使用標簽導航.為此,您需要使用
Besides directional navigation you can use tab navigation. For this you need to use
android:nextFocusForward="@+id/.."
要獲得特定視圖的焦點,請調用
To get a particular view to take focus, call
view.requestFocus()
要監聽某些變化的焦點事件,請使用 View.OnFocusChangeListener
To listen to certain changing focus events use a View.OnFocusChangeListener
您可以使用 android:imeOptions
用于處理鍵盤上的額外按鈕.
You can use android:imeOptions
for handling that extra button on your keyboard.
您可以在與編輯器關聯的 IME 中啟用的其他功能改進與您的應用程序的集成.這里的常數對應于 imeOptions 定義的那些.
Additional features you can enable in an IME associated with an editor to improve the integration with your application. The constants here correspond to those defined by imeOptions.
imeOptions 的常量包括各種動作和標志,請參閱上面的鏈接了解它們的值.
The constants of imeOptions includes a variety of actions and flags, see the link above for their values.
值示例
ActionNext:
操作鍵執行下一步"操作,將用戶帶到下一個接受文本的字段.
the action key performs a "next" operation, taking the user to the next field that will accept text.
ActionDone:
操作鍵執行完成"操作,通常意味著無需輸入任何內容,IME 將關閉.
the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.
代碼示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="16dp"
android:imeOptions="actionNext"
android:maxLines="1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:imeOptions="actionDone"
android:maxLines="1"
android:ems="10" />
</RelativeLayout>
如果您想收聽 imeoptions 事件,請使用 TextView.OnEditorActionListener
.
If you want to listen to imeoptions events use a TextView.OnEditorActionListener
.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
這篇關于在 Android 上單擊軟鍵盤 Next 時移動到另一個 EditText的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!