【发布时间】:2022-12-14 02:57:25
【问题描述】:
我有这个我正在尝试使用的 Android 应用程序,但是当我尝试将软键盘从屏幕隐藏(硬件包括键盘)以用于此特定的警报对话框时,它不会保持隐藏状态,尽管我是按照与之前的警报对话框相同的设置,确实有效。
以下函数 enterItem 的功能完全符合我的预期,这意味着当有人使用硬件输入数据时它不会调出软键盘。
public void enterItem() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View viewInflated = LayoutInflater.from(context).inflate(R.layout.dialog_keyin_number_field, null);
final EditText userInputDialogEditText = viewInflated.findViewById(R.id.keyInNumber);
userInputDialogEditText.setBackgroundColor(getColor(R.color.colorPrimary));
builder
.setTitle(reason.getDescription())
.setMessage("Enter/Scan Item: ")
.setView(viewInflated)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String input = userInputDialogEditText.getText().toString();
if (StringUtils.isNotNullOrEmpty(input)) {
new AsyncVerifyItemWS().execute(input);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});//end builder
AlertDialog dialog = builder.create();
dialog.show();
userInputDialogEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_DONE){
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
return false;
}
});
userInputDialogEditText.setShowSoftInputOnFocus(false);
}
然而,这个功能确实不是功能如我所料。当我开始在硬件键盘上打字时,软键盘会弹出,并且不会随着任何后续点击而消失。
public void enterComment(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View viewInflated = LayoutInflater.from(context).inflate(R.layout.dialog_text_area_field, null);
final EditText userInputDialogEditText = viewInflated.findViewById(R.id.keyInText);
userInputDialogEditText.setBackgroundColor(getColor(R.color.lightGrey));
builder
.setTitle(reason.getDescription())
.setMessage("Enter Comment: ")
.setView(viewInflated)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
comments = userInputDialogEditText.getText().toString();
//move on...
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//move on...
}
});
AlertDialog dialog = builder.create();
dialog.show();
userInputDialogEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId== EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_DONE){
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
}
return false;
}
});
userInputDialogEditText.setShowSoftInputOnFocus(false);
}
除了使用setShowSoftInputOnFocus(false),我还尝试使用userInputDialogEditText.setInputType(InputType.TYPE_NULL);以及以下功能:
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
输入法管理器(位于 onEditorAction() 中)不会改变任何东西,而在使用 setInputType(InputType.TYPE_NULL) 时确实有效,它删除了屏幕上闪烁的位置栏。
我是 Android 开发的新手,与我一起工作的人似乎都不知道如何使这项工作发挥作用,因此我们将不胜感激任何帮助。
【问题讨论】:
标签: android