android获取键盘状态,android软键盘弹出与判断

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;

}

}

});

android:id="@+id/keyboardRelativeLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_height="fill_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:fillViewport="true">

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

方法三:InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

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

更多:更改软键盘的状态,如显示则隐藏如隐藏则显示,取反ToggleInputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

版权声明:

作者信息(Author)  :雪炭网 : 花花世界

原文链接(Hyperlink):https://snowcoal.com/article/641.html

原创内容,尊重版权,转载需注明出处;商业及其他特殊用途转载需原作者同意。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值