Android點擊輸入框以外的地方隱藏輸入框和鍵盤

     原理是監聽整個頁面,然後處理監聽事件。具體見代碼。

     1、在XML文件中定義一個id。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/main_layout"/>
     2、Java文件中設置監聽事件。
    RelativeLayout main_layout=(RelativeLayout)this.findViewById(R.id.main_layout);
    main_layout.setOnClickListener(this);
     3、處理監聽事件。

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.appointment_detail_layout:                        //點擊文本框之外的地方隱藏鍵盤以及輸入框
                InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                rv_input.setVisibility(View.GONE);                                     //這裏是輸入框佈局,設置爲GONE
                inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);     //這裏實現隱藏鍵盤的功能
                break;
        }
    }






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章