我当前正在使用3个编辑文本字段并将其光标向前移动 (edittext1-> edittext2-> edittext3) (当 edittext输入长度== 1
时)我想做相反的方法(删除文本后将光标向后移动 (edittext1 <-edittext2 <-edittext3)),当我按键盘上的退格键
时我的代码是这个
public EditText otp1, otp2, otp3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp_box);
otp1 = findViewById(R.id.otp1);
otp2 = findViewById(R.id.otp2);
otp3 = findViewById(R.id.otp3);
otp1.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (otp1.getText().length() == 1) {
otp2.requestFocus();
}
return false;
}
});
otp2.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (otp2.getText().length() == 1) {
otp3.requestFocus();
}
return false;
}
});
otp3.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (otp3.getText().length() == 1) {
hideSoftKeyboard(v);
return false;
}
return false;
}
});
}
public void hideSoftKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="horizontal">
<EditText
android:id="@+id/otp1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/edit_txt_border_color"
android:gravity="center"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:text=""
android:hint="X"
android:textSize="20sp" />
<EditText
android:id="@+id/otp2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/edit_txt_border_color"
android:gravity="center"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:text=""
android:hint="X"
android:textSize="20sp" />
<EditText
android:id="@+id/otp3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="@drawable/edit_txt_border_color"
android:gravity="center"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:text=""
android:hint="X"
android:textSize="20sp" />
</LinearLayout>
答案 0 :(得分:1)
如果您的addTextChangedListener()
为空,请使用 EditText
,然后将焦点移至上一个EditText
示例代码
尝试一下
otp1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(TextUtils.isEmpty(otp1.getText().toString())){
otp1.requestFocus();
}else {
otp2.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
otp3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(TextUtils.isEmpty(otp2.getText().toString())){
otp2.requestFocus();
}else {
// you logic
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
otp2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(TextUtils.isEmpty(otp2.getText().toString())){
otp1.requestFocus();
}else {
otp3.requestFocus();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:1)
您可以在文本更改监听器中使用requestFocus()
方法:
opt1 = findViewById(R.id.opt1);
opt1.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 (s.toString().length() == 1) {
opt2.requestFocus();
}
}
});
opt2 = findViewById(R.id.opt2);
opt2.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 (s.toString().length() == 1) {
opt3.requestFocus();
}
if (s.toString().length() == 0) {
opt1.requestFocus();
}
}
});
opt3 = findViewById(R.id.opt3);
opt3.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 (s.toString().length() == 0) {
opt2.requestFocus();
}
}
});