ここでは、各編集ボックスにそれぞれ 3、3、4 桁の携帯電話番号を表示し、フォーカスを変更する 3 つの編集ボックスを持つ簡単な例を 1 つ示します。
XML
<EditText
android:id="@+id/edtxt_phonenumber_one"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/textbox_1"
android:ems="3"
android:maxLength="3"
android:gravity="center"
android:inputType="number" />
<EditText
android:id="@+id/edtxt_phonenumber_two"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/textbox_1"
android:ems="3"
android:maxLength="3"
android:gravity="center"
android:inputType="number" />
<EditText
android:id="@+id/edtxt_phonenumber_three"
android:layout_width="wrap_content"
android:layout_height="39dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/textbox_1"
android:ems="4"
android:maxLength="4"
android:gravity="center"
android:inputType="number" />
</LinearLayout>
クラス
//Initialize 3 of EditBox.
// Rest of te code
edtxt_phonenumber1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() > 2) {
edtxt_phonenumber2.requestFocus();
}
if (s.length()==0) {
//previoue_box.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
edtxt_phonenumber2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() > 2) {
edtxt_phonenumber3.requestFocus();
}
if (s.length()==0) {
edtxt_phonenumber1.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
edtxt_phonenumber3.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (s.length() > 3) {
edtxt_email.requestFocus();
}
if (s.length()==0) {
edtxt_phonenumber2.requestFocus();
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
それが役に立てば幸い!!