android软键盘弹出与判断2014-12-02 00:47:45

( 4人已投票,[高质量] )
分享:
31.3K

Android软键盘弹出与判断,Android

一:如何绑定软键盘到EditText:

edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);

二:去除软键盘显示:

editMsgView.setText("");
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

三:始终不弹出软件键盘

EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

也可以:

InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){  //这里可以判断也可以不判断
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );
}


异常处理:软键盘不能弹出,最可能的原因是Android程序未将屏幕绘制完成,所以延迟一定时间,弹出软键盘。

处理方法一:

private Handler hander=new Handler(){
publicvoid handleMessage(android.os.Message msg) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);
};
};


@Override
publicvoid onWindowFocusChanged(boolean hasWindowFocus) {
if(visible){ 
hander.sendEmptyMessageDelayed(0, 1000);
}
}

方法二:

Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
publicvoid run() {
InputMethodManager m = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}, 500);


是否开启状态判断:

方法一:

// 虚拟键盘隐藏 判断view是否为空
View view = getActivity().getWindow().peekDecorView();
if (view != null) {
//隐藏虚拟键盘
InputMethodManager inputmanger = (InputMethodManager) getActivity().getSystemService(MainActivity.INPUT_METHOD_SERVICE);          inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);
}


方法二:

relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);
relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {
public void onKeyboardStateChanged(int state) {
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏
//do sth
break;
case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示
//do sth
break;
default:
break;
}
}
});


<?xml version="1.0" encoding="utf-8"?>
<com.demo.softkeyboard.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</com.demo.softkeyboard.KeyboardListenRelativeLayout>


方法三:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开




更多:更改软键盘的状态,如显示则隐藏如隐藏则显示,取反Toggle

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);







头像

snowcoal
  • Android
  • 软键盘

本文标签:

Android软键盘

收藏到我的私密空间

标题:android软键盘弹出与判断

作者:花花世界

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限