隱藏軟鍵盤
方法一:
> 在 AndroidMainfest.xml中選擇哪個activity,設置windowSoftInputMode屬性為 adjustUnspecified|stateHidden
< activity android:name=".Main"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER" />
< /intent-filter>
< /activity>
方法二:
讓 EditText失去焦點,使用EditText的clearFocus方法
例如:
EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
強制隱藏Android輸入法窗口
例如:((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
RegisterActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
顯示軟鍵盤
//顯示軟鍵盤,控件ID可以是EditText,TextView
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0);
不自動彈出鍵盤
帶有EditText控件的在第一次顯示的時候會自動獲得focus,并彈出鍵盤,如果不想自動彈出鍵盤,有兩種方法:
方法一:在mainfest文件中把對應的activity設置
android:windowSoftInputMode="stateHidden"
或者android:windowSoftInputMode="stateUnchanged"。
方法二:可以在布局中放一個隱藏的TextView,然后在onCreate的時候requsetFocus。
注意TextView不要設置Visiable=gone,否則會失效,可以在布局中放一個隱藏的TextView,然后在onCreate的時候requsetFocus。
<TextView
android:id="@+id/text_notuse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
/>
TextView textView = (TextView)findViewById(R.id.text_notuse);
textView.requestFocus();