【问题标题】:Hide android keyboard when tapping somewhere other than edittext in alertdialog在alertdialog中点击edittext以外的地方时隐藏android键盘
【发布时间】:2014-10-02 11:17:12
【问题描述】:

当我单击不在编辑文本中的某个位置时,我想在警报对话框中隐藏键盘。我一直在 stackoverflow 和 google 上寻找好的答案,但我还没有找到。所以你能帮帮我吗?我有一个带有 4 个编辑文本框的 alertDialog 布局,如果我在它们外面点击,我想关闭键盘。

我希望你们能帮助我,这个不是重复的答案,因为其他人不为我工作,他们也不为 alertdialog。

谢谢你, 干杯:}

【问题讨论】:

  • 检查焦点和隐藏键盘可能会起作用

标签: android keyboard android-edittext android-alertdialog


【解决方案1】:

假设父布局是您拥有 EditText 的 LinearLayout。

所以找到Linear Layout的ID。

linearLayout = (LinearLayout) 来自 XML 的 findViewById(R.id.linearLayout)。

linearLayout.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(v == relAppointment1) {
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context
                                .INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                        return true;
                    }
                    return false;
                }
            });

希望这能解决问题。

【讨论】:

    【解决方案2】:

    @Bhargav Jhaveri

    我编辑了你的代码:

       final Dialog dialog = new Dialog(test.this); //created a dialog
        LinearLayout linearLayout= (LinearLayout) dialog.findViewById(R.id.LinearLayout01); //This is dialog layout
        final EditText editText=(EditText)dialog.findViewById(R.id.edt1);  //this is edittext in dialog
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(v == relAppointment1) { //What is relAppointment1, variable is undefined
                    InputMethodManager imm = (InputMethodManager) getBaseContext().getSystemService(Context
                            .INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
    

    但是什么是 relAppointment1?

    感谢您的回答。请检查我的代码,一切正常吗? 干杯:]

    【讨论】:

      猜你喜欢
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2017-09-27
      • 2012-10-30
      相关资源
      最近更新 更多