ロック画面を作成しようとしています。ロック画面には基本的に4つの編集テキストフィールドがあり、これらのフィールドは基本的に数値入力を受け取り、数値部分を提供するのに問題はありません。
今私が抱えている問題は、テキストフィールドは1つの数値のみを取り、編集テキストフィールドはフォーカスを変更する必要があるということです。1つのフィールドに値が指定された直後に、これをどのように実現できますか?
ロック画面を作成しようとしています。ロック画面には基本的に4つの編集テキストフィールドがあり、これらのフィールドは基本的に数値入力を受け取り、数値部分を提供するのに問題はありません。
今私が抱えている問題は、テキストフィールドは1つの数値のみを取り、編集テキストフィールドはフォーカスを変更する必要があるということです。1つのフィールドに値が指定された直後に、これをどのように実現できますか?
数値のみを入力するには、次のプロパティを使用します
<EditText android:inputType="number" ... />
また
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
For only allowing numerical input (and also showing the "number"-keyboard), use the inputType
-attribute of the EditText class. To only allow entering one digit into the field, you can use the android:maxLength
-attibute.
Use a TextWatcher
to check if the one digit has been entered into the text field and change the focus to the next field.
Add a listener to each EditText for when the text changes (make sure the EditText enforces a max length of 1). The logic of this listener will check that the text is a digit and of length 1, then it'll shift focus. Here's the code:
EditText et1 = ...;
EditText et2 = ...;
EditText et3 = ...;
EditText et4 = ...;
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});
et3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if( s.length() == 1 && Character.isDigit(s.charAt(0)) ) {
et4.requestFocus();
}
}
@Override
public void afterTextChanged(Editable e) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
});