让EditText响应软键盘搜索


给EditText设置软键盘搜索按键:

<EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:imeOptions="actionSearch"
            android:id="@+id/search_txt">


给EditText设置软键盘搜索响应:

view.findViewById(R.id.search_txt).setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {// 修改回车键功能 
					((InputMethodManager)getActivity()
							.getSystemService(Context.INPUT_METHOD_SERVICE))
							.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);// 先隐藏键盘
					getSearchNewsData("search text");	
					return true;
				}
				return false;
			}
		});


你可能感兴趣的:(让EditText响应软键盘搜索)