[Android]搜索界面--自动弹出键盘,并点击键盘搜索进行搜索

来源:互联网 发布:深圳查知科技有限公司 编辑:程序博客网 时间:2024/05/16 14:16

功能分析:

进入搜索页面,自动弹出键盘,并且可以点击键盘搜索按钮进行搜索,并自动隐藏键盘。






1.在XML在输入框中加入android:imeOptions="actionSearch"

 <EditText                android:id="@+id/et_keyword"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_centerVertical="true"                android:layout_toRightOf="@id/icon_keyword"                android:background="@color/transplant"                android:hint="请输入关键字"                android:imeOptions="actionSearch"                android:padding="5dp"                android:singleLine="true"                android:textColor="@color/white"                android:textColorHint="@color/white"                  />
2.自动弹出键盘
et_keyword = (EditText) findViewById(R.id.et_keyword);et_keyword.setSelected(true);et_keyword.requestFocus();//获得焦点
3.然后调用 OnEditorActionListener,不是OnKeyListener(import android.widget.TextView.OnEditorActionListener;

 

<span style="font-size:18px;">et_keyword.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {if (arg1 == EditorInfo.IME_ACTION_SEARCH) { //添加搜索事件                                   hideInput(mContext);//隐藏软键盘                            }return false;}});</span>
/**  * 隐藏软键盘 */          private static InputMethodManager manager;// 输入法管理器 用户隐藏软键盘private void hideInput(Context context) {if(manager==null){manager = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE));}manager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);}


3.在androidMainfest.xml文件中在此Activity中写入 android:windowSoftInputMode="adjustPan"可以防止软键盘会把原来的界面挤上去的问题stateVisible负责让键盘自动弹出

<span style="font-size:18px;">android:windowSoftInputMode="adjustPan|stateVisible"</span>


0 1
原创粉丝点击