在咱們的應用中,有時候一進入一個頁面, EditText默認就會自動獲取焦點。彈出輸入法框,用戶體驗很很差,java
那麼如何取消這個默認行爲呢?android
ps:這篇文字是一年前寫的,如今有網友再問這個問題,我進行從新編輯--2014.05.07,目前有更好的辦法,第一種方法侷限性很強,你們能夠使用第二種方法ide
第一種方法:.在網上找了很久,有點監聽軟鍵盤事件的方法,有調用 clearFouse()方法,可是測試了都不行!在對應的 xml中也找不到相應的屬性能夠關閉這個默認行爲。測試
後來研究了一下,在其父控件下,添加以下的屬性,就能夠完美解決:this
android:focusable="true"
android:focusableInTouchMode="true"spa
舉例以下:日誌
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:focusable
="true" android:focusableInTouchMode="true"code
> <="@+id/et_enter_msg_content"="wrap_content"="wrap_content"="1" /> <="@+id/sent"="wrap_content"="wrap_content"="@string/send" /> </LinearLayout>
第二種方法:直接關閉輸入法orm
1
2
3
4
5
6
7
8
|
private
void
closeInputMethod() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
boolean
isOpen = imm.isActive();
if
(isOpen) {
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//沒有顯示則顯示
imm.hideSoftInputFromWindow(mobile_topup_num.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
|
調用這個方法體就好了,具體if語句裏面的幾個參數,我就借用一個網友的日誌來寫把(在此感謝)xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
1
、方法一(若是輸入法在窗口上已經顯示,則隱藏,反之則顯示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(
0
, InputMethodManager.HIDE_NOT_ALWAYS);
2
、方法二(view爲接受軟鍵盤輸入的視圖,SHOW_FORCED表示強制顯示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
[java] view plaincopy
imm.hideSoftInputFromWindow(view.getWindowToken(),
0
);
//強制隱藏鍵盤
3
、調用隱藏系統默認的輸入法
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.
this
.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是當前的Activity)
4
、獲取輸入法打開的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean
isOpen=imm.isActive();
//isOpen若返回true,則表示輸入法打開
|
好了,祝你們玩的開心