gpt4 book ai didi

Android 如何在单击退格键时聚焦另一个编辑文本

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:16:48 24 4
gpt4 key购买 nike

我正在尝试制作 Pin 进入页面,我创建了 4 个 EditText,并在我的 Activity 下方创建了一个函数。我的问题是,当我单击退格按钮时,我想将当前 EditText 聚焦到 Before(Back) EditText,但 Key 监听器不起作用。

逻辑很简单,当用户输入数字编辑文本时,它失去焦点,下一个编辑文本获得焦点然后就这样了。但问题是当我单击后退空格时,我想在 edittext 之前返回以再次输入数字。我试图在 beforeTextChange 中插入 keyListener,但它不起作用。

 private void SetTextChange(final EditText etCurrent, final EditText etForward,final EditText etBack, final boolean isLast, final int currentPosition)
{

etCurrent.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) {

if (etCurrent.getText().length() == 1 && !isLast ) {
etCurrent.setTypeface(null, Typeface.NORMAL);
etCurrent.clearFocus();
etForward.requestFocus();
etForward.setCursorVisible(true);

etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.lavender_indigo), PorterDuff.Mode.SRC_ATOP);
} else if (etCurrent.length() == 0) {
etCurrent.getBackground().setColorFilter(getResources().getColor(R.color.french_gray), PorterDuff.Mode.SRC_ATOP);
etCurrent.setTypeface(null, Typeface.SANS_SERIF.getStyle());

etCurrent.setTypeface(null, Typeface.NORMAL);
etCurrent.clearFocus();
etBack.requestFocus();
etBack.setCursorVisible(true);

}


if (etCurrent.length() != 0) {
Integer currentKey = Integer.parseInt(etCurrent.getText().toString());
keyList.set(currentPosition, currentKey);
} else
keyList.set(currentPosition, -1);

if (keyList.size() > 3)
showToast(keyList.get(0) + " " + keyList.get(1) + " " + keyList.get(2) + " " + keyList.get(3));

}

@Override
public void afterTextChanged(Editable s) {

}
});

/*curText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View arg0, boolean arg1) {
curText = etCurrent;
backText = etBack;
curText.setText("");
}

});*/
}

这也是我的EditText示例xml之一。(其他同理)

  <EditText
android:layout_column="1"
android:layout_columnWeight="1"
android:id="@+id/etActivationDigit1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:textSize="80dp"
android:textAlignment="center"
android:gravity="center_horizontal"
android:hint="*"
android:textColorHint="@color/french_gray"
android:backgroundTint="@color/french_gray"
android:fontFamily="sans-serif"
android:textColor="@color/perfume"
android:maxLength="1"
android:inputType="number"
/>

我只是想在用户输入错误的数字时删除并返回焦点。注意:当你填写所有字段时,你会看到你可以删除并返回,但我想返回 pin 代码的中间。谢谢。

  etCurrent.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

if(etCurrent.getText().toString() == "" && keyCode == event.KEYCODE_DEL)
{
etCurrent.setText("");
etCurrent.clearFocus();
etBack.requestFocus();
etBack.setText("");
}
return false;
}
});

当我触摸退格键时,我的退格键监听器不会触发,但它会触发所有其他字符。

最佳答案

这对我的 4 位数密码 Activity 有效。

private void setupInputListeners() {
verificationCode1EditText.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) {
if (s.length() == 1) {
verificationCode2EditText.requestFocus(View.FOCUS_DOWN);
}
}

@Override
public void afterTextChanged(Editable s) {
}
});

verificationCode2EditText.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) {
if(count == 0) {
verificationCode1EditText.requestFocus(View.FOCUS_DOWN);
} else {
if (s.length() == 1) {
verificationCode3EditText.requestFocus(View.FOCUS_DOWN);
}
}
}

@Override
public void afterTextChanged(Editable s) {
}
});

verificationCode3EditText.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) {
if(count == 0) {
verificationCode2EditText.requestFocus(View.FOCUS_DOWN);
} else {
if (s.length() == 1) {
verificationCode4EditText.requestFocus(View.FOCUS_DOWN);
}
}
}

@Override
public void afterTextChanged(Editable s) {
}
});

verificationCode4EditText.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) {
if(count == 0) {
verificationCode3EditText.requestFocus(View.FOCUS_DOWN);
}
}

@Override
public void afterTextChanged(Editable s) {
}
});
}

关于Android 如何在单击退格键时聚焦另一个编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33385755/

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