問題描述
當(dāng)我按下下一步"時(shí),用戶 EditText 上的焦點(diǎn)必須移至密碼.然后,從密碼,它必須向右移動(dòng),依此類推.你能幫我寫代碼嗎?
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>
推薦答案
焦點(diǎn)處理
焦點(diǎn)移動(dòng)基于找到最近的算法給定方向的鄰居.在極少數(shù)情況下,默認(rèn)算法可能與開發(fā)人員的預(yù)期行為不匹配.
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 屬性更改定向?qū)Ш降哪J(rèn)行為:
Change default behaviour of directional navigation by using following XML attributes:
android:nextFocusDown="@+id/.."
android:nextFocusLeft="@+id/.."
android:nextFocusRight="@+id/.."
android:nextFocusUp="@+id/.."
除了定向?qū)Ш?,您還可以使用標(biāo)簽導(dǎo)航.為此,您需要使用
Besides directional navigation you can use tab navigation. For this you need to use
android:nextFocusForward="@+id/.."
要獲得特定視圖的焦點(diǎn),請(qǐng)調(diào)用
To get a particular view to take focus, call
view.requestFocus()
要監(jiān)聽某些變化的焦點(diǎn)事件,請(qǐng)使用 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.
您可以在與編輯器關(guān)聯(lián)的 IME 中啟用的其他功能改進(jìn)與您的應(yīng)用程序的集成.這里的常數(shù)對(duì)應(yīng)于 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 的常量包括各種動(dòng)作和標(biāo)志,請(qǐng)參閱上面的鏈接了解它們的值.
The constants of imeOptions includes a variety of actions and flags, see the link above for their values.
值示例
ActionNext:
操作鍵執(zhí)行下一步"操作,將用戶帶到下一個(gè)接受文本的字段.
the action key performs a "next" operation, taking the user to the next field that will accept text.
ActionDone:
操作鍵執(zhí)行完成"操作,通常意味著無(wú)需輸入任何內(nèi)容,IME 將關(guān)閉.
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 事件,請(qǐng)使用 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;
}
});
這篇關(guān)于在 Android 上單擊軟鍵盤 Next 時(shí)移動(dòng)到另一個(gè) EditText的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!