android 软键盘 InputMethodManager

来源:互联网 发布:淘宝会员名取不了 编辑:程序博客网 时间:2024/04/28 15:52

当我们在Android提供的EditText中单击的时候,会自动的弹出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现。我们需要控制软键盘的方式就是两种一个是像EditText那样当发生onClick事件的时候出现软键盘,还有就是当打开某个程序的时候自动的弹出软键盘。

调用下面代码:(第一次调用显示,再次调用则隐藏,如此反复),this指activity

[java:firstline[1]] view plaincopy
  1. InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
  3. imm.showSoftInput(myview, InputMethodManager.SHOW_IMPLICIT);  

 

单独显示隐藏软键盘

显示:

[java:firstline[1]] view plaincopy
  1. InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);  
  2. imm.showSoftInput(myview, 0);  

隐藏:

[java:firstline[1]] view plaincopy
  1. imm.hideSoftInputFromWindow(view.getWindowToken(), 0);  

程序启动后,自动弹出软键盘,可以通过设置一个时间函数来实现,不能再onCreate里写:

[java:firstline[1]] view plaincopy
  1. Timer timer = new Timer();  
  2.   
  3. timer.schedule(new TimerTask() {   
  4. @Override public void run() {   
  5. InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
  6.   
  7. Toast.makeText(chick.this"show", Toast.LENGTH_SHORT).show();   
  8. }   
  9. }, 1000); 


原创粉丝点击