InputMethodManager的用法,鍵盤,隱藏

轉自http://blog.sina.com.cn/s/blog_75992b660101lvtz.html

InputMethodManager imm = (InputMethodManager)getSystemService(SendActivity.this.INPUT_METHOD_SERVICE);   

//隱藏鍵盤   

 imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);    

//顯示鍵盤   

 imm.showSoftInput(editText, 0);  

<wbr><wbr></wbr></wbr>

程序啓動後直接彈出軟鍵盤,不能直接在OnCreate中設置,必須等View繪製事件完畢纔可以彈出,需要用到Timer輔助實現,如果要實現輸入的功能,必須讓EditText獲得焦點。

代碼如下:

//應用啓動後自動打開輸入法

@Override
 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     Timer timer = newTimer();
       timer.schedule(new TimerTask(){
         @Override
              public voidrun() {
                    InputMethodManager imm =(InputMethodManager)MainActivity.this.getSystemService(INPUT_METHOD_SERVICE); 
                    imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS); 
                    Toast.makeText(MainActivity.this, "show",Toast.LENGTH_SHORT).show();
                 }

            }, 1000); //在一秒後打開

}

自動關閉

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(editTextField.getWindowToken(),0);

禁止彈出 

   android:name=".ClientSearchViewActivity"
         android:label="@string/app_name" 
         android:screenOrientation="portrait"
         android:windowSoftInputMode="adjustUnspecified|stateHidden"
         android:configChanges="orientation|keyboardHidden">

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

其他相關問題請訪問  http://www.cnblogs.com/weixing/p/3300908.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章