gpt4 book ai didi

android - 当键盘退格键按下 Android 时,我如何将光标从一个编辑文本移动到另一个编辑文本

转载 作者:行者123 更新时间:2023-11-29 18:45:54 25 4
gpt4 key购买 nike

我目前正在使用 3 个编辑文本字段并将光标向前移动(edittext1--> edittext2--> edittext3)edittext input length==1

我想做它的反向方法(删除文本后向后移动光标 (edittext1<--edittext2<--edittext3)) 当我按下键盘退格键时

输出 Activity look like this

我的代码是这样的

 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>

最佳答案

您可以在文本更改监听器中使用 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();
}
}
});

关于android - 当键盘退格键按下 Android 时,我如何将光标从一个编辑文本移动到另一个编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981140/

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