在AndroidManifest.xml 中的Activity上加 android:windowSoftInputMode="stateVisible|adjustResize",啓動時會自動彈出軟件盤。
如果加載的佈局裏有可編譯組件,如EditText,可以在其前面加一個無長無寬的可編輯組件,這樣啓動時界面不會聚焦到自己有用的控件,除非點擊
1、EditText有焦點(focusable爲true)阻止輸入法彈出
editText=(EditText)findViewById(R.id.txtBody);
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
editText.setInputType(InputType.TYPE_NULL); // 關閉軟鍵盤
return false;
}
});
2、當EidtText無焦點(focusable=false)時阻止輸入法彈出
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
1.調用顯示系統默認的輸入法
方法一、
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(m_receiverView(接受軟鍵盤輸入的視圖(View)),InputMethodManager.SHOW_FORCED(提供當前操作的標記,SHOW_FORCED表示強制顯示));
方法二、
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); (這個方法可以實現輸入法在窗口上切換顯示,如果輸入法在窗口上已經顯示,則隱藏,如果隱藏,則顯示輸入法到窗口上)
2.調用隱藏系統默認的輸入法
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是當前的Activity)
3.獲取輸入法打開的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法打開
原文:http://sunxin1001.iteye.com/blog/854182
http://getcn.net/index.php?mod=skill&action=detail&id=43978
另外
參數 含義
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
軟鍵盤直接覆蓋Activity,通常這是默認值
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
Activity高度會變化,讓出軟鍵盤的空間。和WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN 爲2選1的值
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
Activity一打開就直接顯示軟鍵盤窗口,如果該窗口需要的話(即有EditText,或有ditable的控件)
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
Activity打開後並不直接顯示軟鍵盤窗口,直到用戶自己touch文本框。
一、打開輸入法窗口:
InputMethodManager inputMethodManager�0�2= (InputMethodManager) �0�2 �0�2 �0�2 �0�2 �0�2 �0�2 �0�2 getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受軟鍵盤輸入的編輯文本或其它視圖
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
二、關閉出入法窗口
InputMethodManager inputMethodManager�0�2= (InputMethodManager) �0�2 �0�2 �0�2 �0�2 �0�2 �0�2 �0�2 getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//接受軟鍵盤輸入的編輯文本或其它視圖inputMethodManager
.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
三、如果輸入法打開則關閉,如果沒打開則打開
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
四、獲取輸入法打開的狀態
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法打開