簡體   English   中英

在AlertDialog中點擊除edittext之外的其他地方時隱藏android鍵盤

[英]Hide android keyboard when tapping somewhere other than edittext in alertdialog

當我單擊不在編輯文本中的某個位置時,我想將鍵盤隱藏在警報對話框中。 我一直在stackoverflow和google上找到不錯的答案,但是我還沒有找到。 那你能幫我嗎 我有一個帶有4個edittext框的alertDialog布局,如果要在它們外部點擊,我想關閉鍵盤。

我希望您能為我提供幫助,並且這個答案不會重復,因為其他人不是為我工作,也不是為Alertdialog工作。

謝謝干杯:}

假設父布局是LinearLayout,其中有EditText。

因此,找到線性布局的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;
                }
            });

希望這能解決問題。

@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是什么?

感謝您的回答。 請檢查我的代碼,一切都正確嗎? 干杯:]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM