gpt4 book ai didi

android - 如何在 Android 中创建具有 4 个 block 的 Edittext

转载 作者:行者123 更新时间:2023-11-30 01:44:26 25 4
gpt4 key购买 nike

我在 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 为空白时,我无法捕获 backpress 事件这段代码我也做过

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();
}
}
});

最佳答案

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?

关于android - 如何在 Android 中创建具有 4 个 block 的 Edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33933496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com