Last active
May 16, 2016 09:07
-
-
Save lawrenceching/36cbe0d5773ff24dc238 to your computer and use it in GitHub Desktop.
Activity 启动后自动弹出软键盘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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