当按下键盘退格键 Android 时,我如何将光标从一个编辑文本移动到另一个编辑文本
How i to move cursor from one edit text another when keyboard backspace key pressing Android
我目前正在使用 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>
如果您的 EditText
为空,请使用 addTextChangedListener()
而不是将焦点移至上一个 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) {
}
});
您可以在文本更改侦听器中使用 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();
}
}
});
我目前正在使用 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>
如果您的 EditText
为空,请使用 addTextChangedListener()
而不是将焦点移至上一个 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) {
}
});
您可以在文本更改侦听器中使用 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();
}
}
});