Android事件触发EditText焦点和自动弹出键盘



标签: EditText焦点


本文章已收录于:


分类: 安卓基本控件(2)

作者同类文章 X


关于Android中的EditText焦点与键盘的问题:

通常在一个布局中,给EditText设置焦点方式如下 ,在Activity启动后会自动将焦点定位到第一个EditText并且键盘自动弹起。

方式一,在layout中:


1. android:focusable="true"
2. android:focusableInTouchMode="true"


android:focusable="true"
        android:focusableInTouchMode="true"

方式二,在代码中:



1. edit.setFocusable(true);


edit.setFocusable(true);

如果不需要启动自动弹出键盘,则在manifest文件的Activity标签设置属性android:windowSoftInputMode="stateAlwaysHidden" (或者stateHidden|adjustResize)

根据需要设置来限制不自动弹出键盘。


但是如果要在某个事件触发后要弹出键盘(如从语音切换到文字操作时),需要自动弹出键盘。

具体方法如下:


1. edit.requestFocus();  
2. InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
3. imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);


edit.requestFocus();
			InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);

以上代码主要分两部分:第一步通过requestFocus()方法取得焦点(setFocusable(true)不知道什么原因无效); 




0 0




  • 上一篇Android 屏幕适配
  • 下一篇Gallery3D主界面,自定义XML,子Item有动画效果


我的同类文章


安卓基本控件(2)


  • Gallery3D主界面,自定义XML,子Item有动画效果2015-05-09阅读166
  • android时间控件TimePicker使用实例2015-03-03阅读194

关于Android中的EditText焦点与键盘的问题:

通常在一个布局中,给EditText设置焦点方式如下 ,在Activity启动后会自动将焦点定位到第一个EditText并且键盘自动弹起。

方式一,在layout中:

1. android:focusable="true"
2. android:focusableInTouchMode="true"


android:focusable="true"
        android:focusableInTouchMode="true"

方式二,在代码中:


1. edit.setFocusable(true);


edit.setFocusable(true);

如果不需要启动自动弹出键盘,则在manifest文件的Activity标签设置属性android:windowSoftInputMode="stateAlwaysHidden" (或者stateHidden|adjustResize)

根据需要设置来限制不自动弹出键盘。


但是如果要在某个事件触发后要弹出键盘(如从语音切换到文字操作时),需要自动弹出键盘。

具体方法如下:


1. edit.requestFocus();  
2. InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
3. imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);


edit.requestFocus();
			InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);

以上代码主要分两部分:第一步通过requestFocus()方法取得焦点(setFocusable(true)不知道什么原因无效);