Android 在 KEY_DOWN 上设置下一个 EditText 焦点和可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11133585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set next EditText focused and editable on KEY_DOWN
提问by user1462299
I've got a layout with two editexts. I need to set the second one focused and editable when the enter key is hit on the first one. I've got code for setting focus but I can't start typingwhen the second one gets the focus.
我有一个带有两个编辑文本的布局。当在第一个上按回车键时,我需要将第二个设置为焦点和可编辑。我有设置焦点的代码,但是当第二个获得焦点时我无法开始输入。
PS I also need edittext to be exactly one line height without possibility of making addtional rows. It works as well. pss android 2.3
PS 我还需要 edittext 正好是一行高度,而不能制作额外的行。它也有效。pss 安卓 2.3
XML code:
XML 代码:
<EditText
android:id="@+id/email"
style="@style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="@string/email" android:ems="10" android:lines="1" android:maxLines="1" android:ellipsize="end">
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
style="@style/profileLoginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPassword" android:hint="@string/password">
Java code :
Java代码:
((EditText) findViewById(R.id.email)).setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
((EditText) findViewById(R.id.password)).requestFocus();
return true;
default:
break;
}
}
return false;
}
});
采纳答案by user1462299
It seems to be bugged when running this code on the emulator. However, it works on phones
在模拟器上运行此代码时似乎有问题。但是,它适用于手机
回答by Yevgeny Simkin
You're overthinking this. You can specify the order of the EditText inputs right in the XML without doing anything in Java.
你想多了。您可以直接在 XML 中指定 EditText 输入的顺序,而无需在 Java 中执行任何操作。
Use the android:nextFocusDown
param to specify which field you'd like to get focus when the user hits the enter key.
使用android:nextFocusDown
参数指定当用户按下回车键时您希望获得焦点的字段。
回答by Matthias H
android:inputType="text"
android:nextFocusDown="@+id/..."
worked for me
为我工作
回答by Ankit Agrawal
android:nextFocusDown Worked fine for me But removed the following Method only then its work:
android:nextFocusDown 对我来说工作得很好但是只有在它的工作之后才删除了以下方法:
editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_NEXT) {
editText2.requestFocus();
return false;
}
return false;
}
});
回答by lenooh
Besides adding the + in front of id:
除了在 id 前面添加 + 之外:
android:inputType="text"
android:nextFocusDown="@+id/..."
I also had to add:
我还必须补充:
android:imeOptions="actionNext"