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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章