今天编程碰到了一个问题:有一款平板,打开一个有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