【发布时间】:2019-01-29 13:23:42
【问题描述】:
我目前正在使用 3 个编辑文本字段并将其光标向前移动 (edittext1--> edittext2--> edittext3) 当edittext input length==1
我想做它的反向方法(删除文本后向后移动光标 (edittext1) 当我按下键盘退格键时
我的代码是这样的
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>
【问题讨论】:
标签: android events android-softkeyboard