欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-10 21:52:52
1、//隐藏软键盘    ((inputmethodmanager)getsystemservice(input_method_servic...

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();