Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497542
  • 博文数量: 135
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 905
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-24 19:31
文章分类

全部博文(135)

文章存档

2010年(135)

我的朋友

分类: LINUX

2010-04-09 12:00:40

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;

public class test extends Activity implements OnClickListener {

 Button button;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  LinearLayout me = new LinearLayout(this);
  LinearLayout.LayoutParams my = new LinearLayout.LayoutParams(

  LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT

  );
  button = new Button(this);
  button.setId(123);
  button.setText("ok");
  button.setOnClickListener((android.view.View.OnClickListener) this);
  me.addView(button, my);
  this.setContentView(me);

 }

 public void onClick(View v) {
  
  InputMethodManager m = (InputMethodManager) button.getContext()
    .getSystemService(INPUT_METHOD_SERVICE);
  m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
 }

 @Override
 public void onClick(DialogInterface dialog, int which) {
 }

}

android隐藏以及显示软键盘
  1. //隐藏软键盘   

 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);   

  

//显示软键盘,控件可以是EditText,TextView   

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件, 0);  

     2.利用时间来控制软键盘的弹出实例:

public class MainActivity extends Activity {

private Context context;

private EditText mEditText;

/** Called when the activity is first created. */
@Override
  public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  context = this;
  mEditText = (EditText) findViewById(R.id.EditText01);
  mEditText.requestFocus();

  Timer timer = new Timer();
  timer.schedule(new TimerTask() {

   @Override
                 public void run() {
               InputMethodManager m = (InputMethodManager) context.getSystemService   (Context.INPUT_METHOD_SERVICE);
              m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
       }

                 }, 1000);
       }
}

阅读(1558) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~