//应用启动后自动打开输入法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
InputMethodManager imm =
(InputMethodManager)MainActivity.this.getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
Toast.makeText(MainActivity.this, "show",
Toast.LENGTH_SHORT).show();
}
}, 1000); //在一秒后打开
}
自动关闭:
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
禁止弹出:
查了一下Android SDK的说明,发现可以通过设置Activity的一个属性来解决这个问题,
比如可以在AndroidManifest.xml中这样写:
< activity android:name=".CategoryList"
android:label="@string/app_name"
android:windowSoftInputMode="stateVisible|adjustPan" >
< /activity >
或
android:name=".ClientSearchViewActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustUnspecified|stateHidden"
android:configChanges="orientation|keyboardHidden">
//EditText有焦点阻止输入法弹出
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//记住 EditText的InputType现在是 password
int inType = editText.getInputType(); // backup the input type
editText.setInputType(InputType.TYPE_NULL); // disable soft input
editText.onTouchEvent(event); // call native handler
editText.setInputType(inType); // restore input type
editText.setSelection(editText.getText().length());
return true;
}
});