我在App中有一个选项可以在android中输入4位数的PIN码 为此,我采用了一个线性布局和四个Edittext
喜欢这个
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--<ImageButton-->
<!--android:id="@+id/imageButton3"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:background="#25313f"-->
<!--android:padding="@dimen/dimen_10dp"-->
<!--android:contentDescription="@string/app_name"-->
<!--android:src="@drawable/ic_pwdlogin" />-->
<EditText
android:id="@+id/EdtPin1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="#fff"
android:inputType="numberPassword"
android:maxLength="1"
android:paddingLeft="24dp"
android:singleLine="true"
android:textColor="#111"
android:textColorHint="#FFFFFF" />
<EditText
android:id="@+id/EdtPin2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="#fff"
android:inputType="numberPassword"
android:maxLength="1"
android:paddingLeft="24dp"
android:singleLine="true"
android:textColor="#111"
android:textColorHint="#FFFFFF" />
<EditText
android:id="@+id/EdtPin3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="#fff"
android:inputType="numberPassword"
android:maxLength="1"
android:paddingLeft="24dp"
android:singleLine="true"
android:textColor="#111"
android:textColorHint="#FFFFFF" />
<EditText
android:id="@+id/EdtPin4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="1dp"
android:layout_weight="0.25"
android:background="#fff"
android:inputType="numberPassword"
android:maxLength="1"
android:paddingLeft="24dp"
android:singleLine="true"
android:textColor="#111"
android:textColorHint="#FFFFFF" />
</LinearLayout>
但问题在于此 假设我在第4个edittext中按退格键,然后第一次按退格键,那时它应该保留在同一个edittext中,但是当我再次按退格键时,它应该转到它之前的退格键。 但是当Edittext为空时,我无法捕获反压事件 我也完成了这段代码
final EditText EdtPin3 = (EditText) findViewById(R.id.EdtPin3);
EdtPin3.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable s) {
EditText EdtPin4 = (EditText) findViewById(R.id.EdtPin4);
if (EdtPin4.getText().toString().equals("")) {
EdtPin4.requestFocus();
}
if (EdtPin3.getText().toString().equals("")) {
EdtPin3.requestFocus();
}
}
});
答案 0 :(得分:1)
StringBuilder sb=new StringBuilder();
edtPasscode1.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
if(sb.length()==0&edtPasscode1.length()==1)
{
sb.append(s);
edtPasscode1.clearFocus();
edtPasscode2.requestFocus();
edtPasscode2.setCursorVisible(true);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
if(sb.length()==1)
{
sb.deleteCharAt(0);
}
}
public void afterTextChanged(Editable s) {
if(sb.length()==0)
{
edtPasscode1.requestFocus();
}
}
});
正是您在这里寻找的内容:How to change the focus to next edit text in android?