Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lawrenceching/36cbe0d5773ff24dc238 to your computer and use it in GitHub Desktop.
Save lawrenceching/36cbe0d5773ff24dc238 to your computer and use it in GitHub Desktop.
Activity 启动后自动弹出软键盘
@Override
protected void onResume() {
super.onResume();
final Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
EditText et = (EditText) findViewById(android.R.id.text1);
et.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);
et.setSelection(et.getText().toString().length());
}
};
// 调用 showSoftInput() 可能会因为界面没彻底加载完而弹出失败。所以延迟一段时间后调用保证软键盘弹出成功。
Timer timer = new Timer();
timer.schedule(
new TimerTask() {
public void run() {
handler.sendEmptyMessage(0);
}
}, 500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment