2
代碼運行,並打印出「解僱鍵盤」,但鍵盤不解僱。視圖v只是按鈕和文本框位於相對佈局頂部的背景視圖。我在S3 API運行18鍵盤解僱沒有工作在Android
public View v;
public EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.v = (View) findViewById(R.id.backgroundView);
this.et = (EditText) findViewById(R.id.searchBox);
Button testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("dismissing keyboard");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(getWindow().getCurrentFocus().getWindowToken() ,0);
}
});
this.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("dismissing keyboard");
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(getWindow().getCurrentFocus().getWindowToken() ,0);
}
});
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<View
android:id="@+id/backgroundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<EditText
android:id="@+id/searchBox"
android:layout_width="fill_parent"
android:layout_height= "wrap_content"
android:layout_centerVertical="true"
/>
<Button
android:id="@+id/searchButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchBox"
android:text="Search"
/>
<Button
android:id="@+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/searchBox"
android:text="testButton"
/>
我已經更改爲您建議的那一行,但它仍然沒有關閉鍵盤。也許如果我添加我的佈局文件,它可能會減輕一些光imm.hideSoftInputFromInputMethod(et.getApplicationWindowToken(),0); – 2014-11-09 22:00:42