在这里,我给你一个简单的例子,它有 3 个编辑框,分别向每个编辑框显示手机号码 3、3、4 位并更改焦点。
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) {
}
});
希望能帮助到你!!