【问题标题】:Android soft keyboard not staying hiddenAndroid软键盘不隐藏
【发布时间】: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


    【解决方案1】:

    在找出更好的方法来研究我的问题后回答我自己的问题。我个人不喜欢这个答案。最后一个 EditText 小部件的工作方式与 Activity 中的其他小部件不同,这对我来说没有意义,但是通过向对象添加 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;
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2011-02-17
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2012-10-28
      • 2012-05-18
      • 1970-01-01
      相关资源
      最近更新 更多