动态显示和隐藏软键盘

package oyc.com; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class hello extends Activity { private TextView mTextView01; private EditText mEditText01; private InputMethodManager m; private Timer timer; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView01 = (TextView)findViewById(R.id.myTextView); mEditText01 = (EditText)findViewById(R.id.myEditText); mEditText01.setVisibility(View.GONE); m = (InputMethodManager)mEditText01.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); timer = new Timer(); timer.schedule(new TimerTask(){ @Override public void run() { m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //Toast.makeText(hello.this, "show", Toast.LENGTH_SHORT).show(); } }, 1000); //显示键盘 findViewById(R.id.button_show_keyboard).setOnClickListener(new OnClickListener() { public void onClick(View v) { //m.showSoftInput(mEditText01, InputMethodManager.HIDE_NOT_ALWAYS); m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); /*timer.purge(); timer.schedule(new TimerTask(){ @Override public void run() { m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); //Toast.makeText(hello.this, "show", Toast.LENGTH_SHORT).show(); } }, 1000);*/ } }); //隐藏键盘 findViewById(R.id.button_hide_keyboard).setOnClickListener(new OnClickListener() { public void onClick(View v) { ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow( mEditText01.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { mTextView01.setText("" + keyCode); switch (keyCode) { case KeyEvent.KEYCODE_0: break; default: break; } return super.onKeyDown(keyCode, event); } } 

你可能感兴趣的:(动态显示和隐藏软键盘)