點擊空白處自動隱藏輸入法這個需求很常見,最近也要用到,但是需要處理的頁面很零散的幾個。看到網上的有很多種方法,但是在fragment上會無效,後來發現一中思路,其實也很簡單,就是監聽需要處理界面的父佈局做處理就可以了。理論上無論是哪裏都適用。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick">
<EditText
android:id="@+id/ed_dns_2"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
android:background="@null"
android:text=""
android:textSize="16px"
android:hint="自動獲取"
android:textColor="@color/colorBlack"
android:paddingLeft="10px"
android:paddingRight="10px"
android:enabled="false"
/>
</RelativeLayout>
onClick處理方法:
private InputMethodManager imm;
public void onClick(View v) {
if (null == imm) {
imm = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
}
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
這樣就可以了!!!!!如果測試無效的話請留言我改正 謝謝大家