今天編程碰到了一個問題:有一款平板,打開一個有EditText的Activity會默認彈出輸入法。爲了解決這個問題就深刻研究了下android中焦點Focus和彈出輸入法的問題。在網上看了些例子都不夠全面,在這裏全面總結下。html
一:EditText爲何會默認彈出輸入法?android
一樣的代碼,碰到有EditText控件的界面時有的機子會彈出輸入法,有的機子不會彈出。很差意思,這問題我也一頭霧水,誰知道能夠告訴我,不然我就把這個問題留下來,之後研究android源碼時再搞個清楚。可是...我有解決方案。編程
二:默認彈出和默認關閉輸入法的解決方案。ide
1.默認關閉,不至於進入Activity就打開輸入法,影響界面美觀。函數
①在佈局文件中,在EditText前面放置一個看不到的LinearLayout,讓他率先獲取焦點:佈局
<LinearLayoutpost
android:focusable="true"this
android:focusableInTouchMode="true"spa
android:layout_width="0px".net
android:layout_height="0px"/>
②方法二:先看一個屬性android:inputType:指定輸入法的類型,int類型,能夠用|選擇多個。取值能夠參考:android.text.InputType類。取值包括:text,textUri, phone,number,等.
Android SDK中有這麼一句話「If the given content type is TYPE_NULL
then a soft keyboard will not be displayed for this text view」,
先將EditText的InputType改變爲TYPE_NULL,輸入法就不會彈出.而後再設置監聽,再從新設置它的InputType.
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft input
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
return true;
}
});
2.默認彈出。有時候按照需求可能要求默認彈出輸入法。方案以下:
EditText titleInput = (EditText) findViewById(R.id.create_edit_title);
titleInput.setFocusable(true);
titleInput.requestFocus();
onFocusChange(titleInput.isFocused());
private void onFocusChange(boolean hasFocus)
{
final boolean isFocus = hasFocus;
(new Handler()).postDelayed(new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager)
titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(isFocus)
{
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
else
{
imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0);
}
}
}, 100);
}
我以爲由於在Android的主線程中對UI的操做無效,因此必須在Handler中實現彈出輸入法的操做。
三。關於焦點和輸入法的我的理解
獲取焦點是獲取焦點,彈輸入法是彈輸入法。獲取焦點後並不必定會彈出輸入法,在網上搜了一圈,主流回答是「還有就是已開啓界面就是focus的text的話有可能也是不行的,UI渲染是須要時間的」......
因爲對源碼不懂,我對這一點也沒有個全面的認識。只能將焦點和輸入法分紅兩塊來處理。焦點的打開和關閉特別簡單。
焦點的獲取:
titleInput.setFocusable(true);
titleInput.requestFocus();
焦點的取消:
titleInput.setFocusable(false);
四。關於常常調用的處理軟鍵盤的函數以下:<轉載>
一、打開輸入法窗口:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受軟鍵盤輸入的編輯文本或其它視圖
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
二、關閉出入法窗口
InputMethodManager inputMethodManager = (InputMethodManager) 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,則表示輸入法打開
能夠轉載,但請註明出處,謝謝!
做者:Carman 2012-07-30 20:03:06
郵箱:carman_loneliness@163.com
參考文章:http://blog.csdn.net/dengta_snowwhite/article/details/6427035