这是输入数字密码的4 EditText
。我希望它是这样的,如果第一个EditText
被1个数字填充,那么焦点应该转到下一个EditText
,并且也应该以相反的方式工作。这样用户可以继续从最左边输入密码,也可以从最右边删除相同的方式。
有人可以建议什么是最佳方法吗?
答案 0 :(得分:3)
您不能单独使用addTextChangedListener完成它。您可能必须同时设置onKeyListener。这是给您的代码:
//6 EditTexts are otpEt[0], otpEt[1],...otpEt[5]
private EditText[] otpEt = new EditText[6];
otpEt[0] = (EditText) findViewById(R.id.otpEt1);
otpEt[1] = (EditText) findViewById(R.id.otpEt2);
otpEt[2] = (EditText) findViewById(R.id.otpEt3);
otpEt[3] = (EditText) findViewById(R.id.otpEt4);
otpEt[4] = (EditText) findViewById(R.id.otpEt5);
otpEt[5] = (EditText) findViewById(R.id.otpEt6);
setOtpEditTextHandler();
private void setOtpEditTextHandler () { //This is the function to be called
for (int i = 0;i < 6;i++) { //Its designed for 6 digit OTP
final int iVal = i;
otpEt[iVal].addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(iVal == 5 && !otpEt[iVal].getText().toString().isEmpty()) {
otpEt[iVal].clearFocus(); //Clears focus when you have entered the last digit of the OTP.
} else if (!otpEt[iVal].getText().toString().isEmpty()) {
otpEt[iVal+1].requestFocus(); //focuses on the next edittext after a digit is entered.
}
}
});
otpEt[iVal].setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN) {
return false; //Dont get confused by this, it is because onKeyListener is called twice and this condition is to avoid it.
}
if(keyCode == KeyEvent.KEYCODE_DEL &&
otpEt[iVal].getText().toString().isEmpty() && iVal != 0) {
//this condition is to handel the delete input by users.
otpEt[iVal-1].setText("");//Deletes the digit of OTP
otpEt[iVal-1].requestFocus();//and sets the focus on previous digit
}
return false;
}
});
}
}
如果您觉得这段代码很困难,只需将其粘贴到您的项目中,然后尝试重新排列即可。您将可以轻松获得它
答案 1 :(得分:2)
如果您熟悉cv2.GaussianBlur(src, ksize=(1,1))
,那么这可能是满足您需求的最简单方法。
以下是RxJava
代码
Kotlin
你也可以用同样的方式写反向。长度为零时,焦点与前一个Edittext相同。
答案 2 :(得分:1)
您可以使用库Android PinView / OtpView
或者您可以使用addTextChangedListener
添加TextWatcher
,只要此EditTextView文本发生更改,就会调用View.requestFocus()
,然后您可以在下一个EditText上调用body{
background-color: #ddd;
}
.toggle {
-webkit-appearance: none;
appearance: none;
width: 65.57px;
padding: 10px;
height: 26.32px;
display: inline-block;
position: relative;
border-radius: 13px;
overflow: hidden;
line-height: 16px;
text-align: center;
font-size: 12px;
outline: none;
border: none;
cursor: pointer;
background: #E1F6FF;
transition: background-color ease 0.3s;
}
.toggle:before {
content: "text text";
display: block;
position: absolute;
z-index: 2;
width: 24px;
height: 24px;
background: #fff;
left: 0;
top: 0;
border-radius: 50%;
padding-top: 5px;
text-indent: -30px;
word-spacing: 37px;
color: #4D5585;
text-shadow: -1px -1px rgba(0,0,0,0.15);
white-space: nowrap;
box-shadow: 0 1px 2px rgba(0,0,0,0.2);
transition: all cubic-bezier(0.3, 1.5, 0.7, 1) 0.3s;
}
.toggle:checked {
background: #dadce8;
}
.toggle:checked:before {
left: 40px;
}
.center{
text-align: center;
}
来关注它
答案 3 :(得分:0)
您可以做到
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<EditText
android:id="@+id/otpET1"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/otpET2"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/otpET3"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/otpET4"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/otpET5"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:maxLength="1"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/otpET6"
android:layout_width="30dp"
android:layout_height="30dp"
android:inputType="number"
android:gravity="center"
android:maxLength="1"
android:textSize="20sp"/>
</LinearLayout>
活动中
EditText[] otpETs = new EditText[6];
otpETs[0] = findViewById(R.id.otpET1);
otpETs[1] = findViewById(R.id.otpET2);
otpETs[2] = findViewById(R.id.otpET3);
otpETs[3] = findViewById(R.id.otpET4);
otpETs[4] = findViewById(R.id.otpET5);
otpETs[5] = findViewById(R.id.otpET6);
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
if (keyCode == 7 || keyCode == 8 ||
keyCode == 9 || keyCode == 10 ||
keyCode == 11 || keyCode == 12 ||
keyCode == 13 || keyCode == 14 ||
keyCode == 15 || keyCode == 16 ||
keyCode == 67) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KEYCODE_DEL) {
int index = checkWhoHasFocus();
if (index != 123) {
if (Helpers.rS(otpETs[index]).equals("")) {
if (index != 0) {
otpETs[index - 1].requestFocus();
}
} else {
return super.dispatchKeyEvent(event);
}
}
} else {
int index = checkWhoHasFocus();
if (index != 123) {
if (Helpers.rS(otpETs[index]).equals("")) {
return super.dispatchKeyEvent(event);
} else {
if (index != 5) {
otpETs[index + 1].requestFocus();
}
}
}
return super.dispatchKeyEvent(event);
}
}
} else {
return super.dispatchKeyEvent(event);
}
return true;
}
private int checkWhoHasFocus() {
for (int i = 0; i < otpETs.length; i++) {
EditText tempET = otpETs[i];
if (tempET.hasFocus()) {
return i;
}
}
return 123;
}
这只是从editTexts中获取字符串
public class Helpers {
public static String rS(EditText editText) {
return editText.getText().toString().trim();
}
}
最后,
String code = Helpers.rS(otpETs[0]) + Helpers.rS(otpETs[1]) +
Helpers.rS(otpETs[2]) + Helpers.rS(otpETs[3]) + Helpers.rS(otpETs[4])
+ Helpers.rS(otpETs[5]);
或仅使用简单的for/while
循环。
答案 4 :(得分:0)
在科特林,您可以像..
这样使用波纹管txtOTP_1.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_1.text.toString().length == 1) {
txtOTP_1.clearFocus()
txtOTP_2.requestFocus()
txtOTP_2.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_1.text.toString().length == 0) {
txtOTP_1.requestFocus()
}
}
})
txtOTP_2.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_2.text.toString().length == 1) {
txtOTP_2.clearFocus()
txtOTP_3.requestFocus()
txtOTP_3.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_2.text.toString().length == 0) {
txtOTP_2.requestFocus()
}
}
})
txtOTP_3.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_3.text.toString().length == 1) {
txtOTP_3.clearFocus()
txtOTP_4.requestFocus()
txtOTP_4.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_3.text.toString().length == 0) {
txtOTP_3.requestFocus()
}
}
})
txtOTP_4.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_4.text.toString().length == 1) {
txtOTP_4.clearFocus()
txtOTP_5.requestFocus()
txtOTP_5.setCursorVisible(true)
}
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
if (txtOTP_4.text.toString().length == 0) {
txtOTP_4.requestFocus()
}
}
})
txtOTP_5.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (txtOTP_5.text.toString().length == 1) {
txtOTP_5.requestFocus()
txtOTP_5.setCursorVisible(true)
}
}
})