您的位置:首页 > 移动开发 > Android开发

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

2014-09-05 14:34 477 查看
功能分析:

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

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

@Override
public 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐