今天在做搜索框的时候、遇到需要获取焦点之后做一些事情、实现方法也很简单、那就是绑定OnFocusChangeListener事件、实现 onFocusChange(View v, boolean hasFocus) 方法、第二个参数就是判断得到焦点或失去焦点、从而实现我得想要的效果、代码如下:
EditText searchView = (EditText) findViewById(R.id.search_text);searchView.setOnFocusChangeListener(new android.view.View. OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { // 此处为得到焦点时的处理内容 } else { // 此处为失去焦点时的处理内容 } }});
文本框重新获得焦点方法如下
searchView.setFocusable(true);searchView.setFocusableInTouchMode(true);searchView.requestFocus(); searchView.clearFocus();//失去焦点searchView.requestFocus();//获取焦点
EditText et = (EditText) findViewById(R.id.et); et.clearFocus(); et.setFocusable(false);
searchEdit.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub searchEdit.setFocusable(true); searchEdit.setFocusableInTouchMode(true); searchEdit.requestFocus(); Intent intent = new Intent(getActivity(),SearchActivity.class); startActivity(intent); return false; } });