android如何调用显示和隐藏系统默认的输入法

591 阅读1分钟

android如何调用显示和隐藏系统默认的输入法


1.调用显示系统默认的输入法


方法一、


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);


imm.showSoftInput(m_receiverView(接受软键盘输入的视图(View)),InputMethodManager.SHOW_FORCED(提供当前操作的标记,SHOW_FORCED表示强制显示));


方法二、


InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); (这个方法可以实现输入法在窗口上切换显示,如果输入法在窗口上已经显示,则隐藏,如果隐藏,则显示输入法到窗口上)


2.调用隐藏系统默认的输入法


((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity是当前的Activity)


3.获取输入法打开的状态


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





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