Android 动态显示和隐藏软键盘

**
 * 动态设置软盘的显示和隐藏
 * @author JPH
 */
public class MainActivity extends Activity implements OnClickListener {

    private InputMethodManager inputMethodManager;
	private Timer timer;
	private EditText edit;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edit=(EditText)findViewById(R.id.edit);
        inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
        timer = new Timer();  
        timer.schedule(new TimerTask(){  
            @Override  
            public void run() {  
                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
            }  
        }, 1000);  
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnShow://显示软盘
			inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
			break;
		case R.id.btnHide://隐藏软盘			  
			inputMethodManager.hideSoftInputFromWindow(edit.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);    
			break;	

		default:
			break;
		}
	}  
  
}  

你可能感兴趣的:(Android软盘,设置软盘的显示和隐藏)