看一個(gè)manifest中Activity的配置,如果這個(gè)頁(yè)面有EditText,并且我們想要進(jìn)入這個(gè)頁(yè)面的時(shí)候默認彈出輸入法,可以這樣設置這個(gè)屬相:android:windowSoftInputMode=stateVisible,這樣就會(huì )默認彈起輸入法,當然還有別的辦法。<activity android:name=".ui.login"
android:configChanges="orientation|keyboardHidden|locale"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustPan" >
</activity>
Android EditText 不彈出輸入法總結
方法一:
在A(yíng)ndroidMainfest.xml中選擇哪個(gè)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失去焦點(diǎn),使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
強制隱藏Android輸入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
2.EditText始終不彈出軟件鍵盤(pán)
例:EditText edit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
研究了下android中焦點(diǎn)Focus和彈出輸入法的問(wèn)題。在網(wǎng)上看了些例子都不夠全面,在這里全面總結下。
一:EditText為什么會(huì )默認彈出輸入法?
同樣的代碼,碰到有EditText控件的界面時(shí)有的機子會(huì )彈出輸入法,有的機子不會(huì )彈出。不好意思,這問(wèn)題我也一頭霧水,誰(shuí)知道可以告訴我,否則我就把這個(gè)問(wèn)題留下來(lái),以后研究android
源碼時(shí)再搞個(gè)清楚。但是...我有解決方案。
二:默認彈出和默認關(guān)閉輸入法的解決方案。
1.默認關(guān)閉,不至于進(jìn)入Activity就打開(kāi)輸入法,影響界面美觀(guān)。
?、僭诓季治募?,在EditText前面放置一個(gè)看不到的LinearLayout,讓他率先獲取焦點(diǎn):
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
?、诜椒ǘ合瓤匆粋€(gè)屬性android:inputType:指定輸入法的類(lèi)型,int類(lèi)型,可以用|選擇多個(gè)。取值可以參考:android.text.InputType類(lèi)。取值包括:text,textUri,
phone,number,等.
the given content type is TYPE_NULL
then a soft keyboard will not be displayed for this text view”,
先將EditText的InputType改變?yōu)門(mén)YPE_NULL,輸入法就不會(huì )彈出.然后再設置監聽(tīng),再重新設置它的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.默認彈出。有時(shí)候按照需求可能要求默認彈出輸入法。方案如下:
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);
}
我覺(jué)得因為在A(yíng)ndroid的主線(xiàn)程中對UI的操作無(wú)效,所以必須在Handler中實(shí)現彈出輸入法的操作。
三。關(guān)于焦點(diǎn)和輸入法的個(gè)人理解
獲取焦點(diǎn)是獲取焦點(diǎn),彈輸入法是彈輸入法。獲取焦點(diǎn)后并不一定會(huì )彈出輸入法,在網(wǎng)上搜了一圈,主流回答是“還有就是已開(kāi)啟界面就是focus的text的話(huà)有可能也是不行的,UI渲染是需要時(shí)間的”......
由于對源碼不懂,我對這一點(diǎn)也沒(méi)有個(gè)全面的認識。只能將焦點(diǎn)和輸入法分成兩塊來(lái)處理。焦點(diǎn)的打開(kāi)和關(guān)閉特別簡(jiǎn)單。
焦點(diǎn)的獲?。?/div>
titleInput.setFocusable(true);
titleInput.requestFocus();
焦點(diǎn)的取消:
titleInput.setFocusable(false);
四。關(guān)于經(jīng)常調用的處理軟鍵盤(pán)的函數如下:<轉載>
1、打開(kāi)輸入法窗口:
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// 接受軟鍵盤(pán)輸入的編輯文本或其它視圖
imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
2、關(guān)閉出入法窗口
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//接受軟鍵盤(pán)輸入的編輯文本或其它視圖
inputMethodManagerwww.2cto.com
.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);
3、如果輸入法打開(kāi)則關(guān)閉,如果沒(méi)打開(kāi)則打開(kāi)
InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
4、獲取輸入法打開(kāi)的狀態(tài)
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen若返回true,則表示輸入法打開(kāi)