【发布时间】:2012-01-09 06:06:31
【问题描述】:
我有一个用户名和一个密码 XML 段
<EditText
android:id="@+id/username"
android:hint="@string/username_hint"
android:layout_width="match_parent"
android:maxLength="9"
android:nextFocusDown="@+id/pswd"
android:layout_height="wrap_content">
</EditText>
<EditText
android:inputType="textPassword"
android:hint="@string/password_hint"
android:id="@+id/pswd"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</EditText>
我希望当用户名字段到达第 9 个字符时光标焦点自动跳转到密码字段
我正在查看Focus next textview automatically 并尝试:
final EditText usr = (EditText) findViewById (R.id.username);
final EditText pswd = (EditText) findViewById (R.id.pswd);
usr.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 9) {
pswd.requestFocus();
}
}
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
});
没有太大的成功。我在处理我的登录信息的oncreate 方法中使用它。任何想法或建议将不胜感激。
【问题讨论】:
-
尝试这段代码会发生什么?
标签: android focus android-edittext