使用InputMethodManager即可。
主要代码如下(亲测可用,仅供参考):
public class MainActivity extends Activity {
private Boolean isShow = false ;
EditText editText;
InputMethodManager imm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
Button showHideBtn = (Button) findViewById(R.id.show_hide_btn);
showHideBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isShow) {
isShow = false;
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
} else {
isShow = true; imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}
});
}
}
官方文档如下:http://developer.android.com/referenc...