所以我有3个编辑文本框, 当用户按下其中的一个输入时, 我想继续下一个。
我试过了:
final EditText email = (EditText) findViewById(R.id.editText1);
email.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.editText2).requestFocus();
return true;
}
return false;
}
});
final EditText pass = (EditText) findViewById(R.id.editText2);
pass.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.editText3).requestFocus();
return true;
}
return false;
}
});
final EditText passrep = (EditText) findViewById(R.id.editText3);
passrep.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.button1).requestFocus();
return true;
}
return false;
}
});
但使用此代码, 焦点会从编辑Text1跳到键盘输入的编辑Text3 。
这个工作(我偶然发现)是什么呢?
final EditText email = (EditText) findViewById(R.id.editText1);
email.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.editText2).requestFocus();
return true;
}
return false;
}
});
final EditText pass = (EditText) findViewById(R.id.editText2);
email.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.editText3).requestFocus();
return true;
}
return false;
}
});
final EditText passrep = (EditText) findViewById(R.id.editText3);
email.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
findViewById(R.id.button1).requestFocus();
return true;
}
return false;
}
});
例如,为同一份《编辑图》(电子邮件)设置三个不同的OnKeyListeners。
我错过什么了 这是我的布局
<EditText
android:id="@+id/editText1"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:inputType="textEmailAddress"
android:layout_marginTop="20dp"
android:hint="Email" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:inputType="textPassword"
android:layout_marginTop="20dp"
android:hint="Password" />
<EditText
android:id="@+id/editText3"
android:layout_width="234dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:inputType="textPassword"
android:layout_marginTop="20dp"
android:hint="Confirm Password" />
<Button
android:id="@+id/button1"
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:text="@string/submit" />