Reputation: 763
I've some deprecated code I need to fix. After a dialog (showdialog) gets dismissed I want an edittext to get focused, text selected and keyboard up. That does not happen with this code.
With this code, there is no focus but the text gets selected.
...
alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
EditText txtE = (EditText) findViewById(R.id.ORE);
dismissDialog(DIALOG_NO_E);
txtE.selectAll();
txtE.requestFocus();
}
});
...
Suggestions?
Upvotes: 1
Views: 598
Reputation: 763
Adding this after requestfocus made it work however. But it does not feel right.
txtE.postDelayed(new Runnable() {
public void run() {
android.view.inputmethod.InputMethodManager keyboard = (android.view.inputmethod.InputMethodManager) getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtE, 0);
}
}, 200);
End result
...
alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dismissDialog(DIALOG_NO_E);
txtE.selectAll();
txtE.requestFocus();
}
});
txtE.postDelayed(new Runnable() {
public void run() {
android.view.inputmethod.InputMethodManager keyboard = (android.view.inputmethod.InputMethodManager) getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(txtE, 0);
}
}, 200);
...
If someone has a better solution or an answer to why I have to do it like this, I'll give you the points for the answer latest on monday.
For this to work thou, I had to create the txtE
as a static variable initialized in the constructor. That didnt feel good at all in my soul. So, if anyone has a better idea that works, do tell.
Upvotes: 1