android 虚拟键盘的显示与隐藏问题

焦点改变时如何隐藏虚拟键盘
在开发过程中碰到了一个需求:在点击搜索框后,再点击别的地方,让虚拟键盘隐藏


在焦点改变时,隐藏虚拟键盘。在 listView 中添加焦点监听。其中 mEditText 为所输入的文本。
import android.view.MotionEvent;
     private void initListView() {
          mAdapter = WaterMarkLocationAdapter.getInstance(this);
          mListView.setAdapter(mAdapter);
          mListView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView adapter, 
                         View view, int position, long id) {
                    String text = (String)mAdapter.getItem(position);
                    if (text == null) return;
                    //mEditText.setText(text);
                    save2pref(text);
                    WaterMarkLocationActivity.this.finish();
               }
          });
          mListView.setOnFocusChangeListener(new View.OnFocusChangeListener(){
               public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                         hideInputSoft();
                    }
               }
          });
          mListView.setOnTouchListener(new View.OnTouchListener() {
               @Override
               public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                         v.requestFocus();
                    }
                    return false;
               } 
          });
     }
     
     public void hideInputSoft() {
          InputMethodManager imm = InputMethodManager.peekInstance();
          if (imm != null)
               imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
     }

那么显示输入法虚拟键盘也是一样:
InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
发布了112 篇原创文章 · 获赞 32 · 访问量 141万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章