Android软键盘没有隐藏



我有这个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开发非常陌生,我的工作中似乎没有人有任何想法如何使这项工作,所以任何帮助将非常感激。

在找到更好的方法来研究我的问题后回答我自己的问题。我个人不喜欢这个答案。对我来说,最后一个EditText小部件的工作方式与活动中的其他小部件不同,这是没有意义的,但是通过向对象添加OnKeyListener(),我可以完全防止软键盘出现在屏幕上。每次点击,屏幕底部仍然有一个闪烁的图标,但是软键盘仍然隐藏。

userInputDialogEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return false;
}
});