我有一个Edittext字段,希望用户能够非常快速地输入文本。将输入的文本以大写字母开头,后跟一组数字。因此,在输入第一个字母后,我希望键盘视图切换为仅显示数字。这是我的edittext监听器代码:
final EditText carPlate=(EditText) findViewById(R.id.carPlateNumber);
carPlate.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
// if keydown and "enter" is pressed
if ((event.getAction()==KeyEvent.ACTION_DOWN)
&& (keyCode==KeyEvent.KEYCODE_ENTER)) {
// display a floating message
DisplayToast(carPlate.getText().toString() + carPlateCharnum);
carPlateCharnum++;
return true;
}
return false;
}
});
问题是我按下回车键后键盘视图才会切换。所以我需要知道在哪里以及如何放置这部分代码:
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
此外,我不希望插入点在输入第一个字母后返回到EditText字段的开头。