解析android中隐藏与显示软键盘及不自动弹出键盘的实现方法

1、//隐藏软键盘   

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);   

  

2、//显示软键盘,控件ID可以是EditText,TextView   

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0); 

 

3、不自动弹出键盘:

带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法:

方法一:在mainfest文件中把对应的activity设置

android:windowSoftInputMode="stateHidden" 或者android:windowSoftInputMode="stateUnchanged"。

方法二:可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus。

注意TextView不要设置Visiable=gone,否则会失效

,可以在布局中放一个隐藏的TextView,然后在onCreate的时候requsetFocus

注意TextView不要设置Visiable=gone,否则会失效

<TextView

        android:id="@+id/text_notuse"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:focusable="true"

android:focusableInTouchMode="true"

        />

 

TextView textView = (TextView)findViewById(R.id.text_notuse);

            textView.requestFocus();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android的Dialog关闭键盘需要以下步骤: 1. 首先,获取Dialog的Window对象。可以通过`dialog.getWindow()`方法实现。 2. 接下来,调用Window的`setSoftInputMode()`方法,将其参数设置为`WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN`。这将会使得键盘始终不会自动弹。 3. 最后,调用Window的`setContentView()`方法加载布局文件或者视图对象。 代码示例: ```java Dialog dialog = new Dialog(context); Window window = dialog.getWindow(); window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); window.setContentView(R.layout.dialog_layout); ``` 在Android的Dialog控制键盘显示隐藏,可以通过以下方式实现: 1. 如果Dialog有EditText控件需要获取焦点时,可以使用`InputMethodManager`类来显示键盘。可以在控件获得焦点时,调用`InputMethodManager`的`showSoftInput()`方法显示键盘。 代码示例: ```java EditText editText = dialog.findViewById(R.id.edit_text); editText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); ``` 2. 如果需要在特定情况下隐藏键盘,可以使用`InputMethodManager`类的`hideSoftInputFromWindow()`方法。 例如,当失去焦点或者点击Dialog外部区域时,可以调用`hideSoftInputFromWindow()`方法隐藏键盘。 代码示例: ```java View rootView = dialog.findViewById(R.id.root_view); rootView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } return false; } }); ``` 通过以上步骤,可以在Android的Dialog关闭键盘以及控制键盘显示隐藏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值