[英]Cursor at wrong position when resizing EditText with animateLayoutChanges
[英]EditText Focus and Cursor Selection is Inputting to the Wrong Input Field
我的Android應用程序中有兩個EditText字段。 當第一個字段中的輸入無效時,將觸發警報對話框,我嘗試將焦點和輸入光標切換回無效的EditText字段(RollId),以便可以重新輸入。
焦點更改和輸入光標實際上似乎起作用了, 但是當我再次開始輸入輸入時,所有這些都顯示在錯誤的editText(Location)字段中。
final EditText RollId = findViewById(R.id.scan_roll_id);
final EditText Location = findViewById(R.id.scan_location);
RollId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
if (validate.ValidateRollId(String.valueOf(RollId.getText()))) {
// TODO: parse and populate labels
} else {
// Not a valid RollId format
AlertDialog alertDialog = new AlertDialog.Builder(MoveMaterialActivity.this).create();
validate.RollIdAlert("The scanned Roll Id has the incorrect format. Please enter manually.", alertDialog);
TextKeyListener.clear(RollId.getText());
RollId.setSelection(0);
Location.clearFocus();
RollId.requestFocus();
}
}
}
});
我想我基本上是想弄清楚如何阻止從Location EditText字段輸入,或將其切換到RollId EditText字段。 我認為RollId.setSelection(0)
將實現這一目標。
編輯: RollIdAlert())
public void RollIdAlert(String message, AlertDialog alert) {
alert.setTitle("RollId Format Invalid");
alert.setMessage(message);
alert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
修改您的validate.RollIdAlert()方法,使其包含3d參數: EditText et
在您的監聽器中,在dismiss();
之后dismiss();
警報對話框dismiss();
,將這些行:
et.requestFocus();
et.setSelection(0);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.