Android 在dismissDialog后将焦点设置为EditText
我有一些不推荐使用的代码需要修复。 在一个对话框(showdialog)被取消后,我想让一个edittext获得焦点,选中文本并启动键盘。这段代码不会发生这种情况 使用此代码,没有焦点,但文本被选中Android 在dismissDialog后将焦点设置为EditText,android,Android,我有一些不推荐使用的代码需要修复。 在一个对话框(showdialog)被取消后,我想让一个edittext获得焦点,选中文本并启动键盘。这段代码不会发生这种情况 使用此代码,没有焦点,但文本被选中 ... alert.setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterfac
...
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();
}
});
...
建议 但是,在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);
最终结果
...
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);
...
如果有人有更好的解决方案或答案来解释我为什么要这样做,我会在周一给你打分
为了实现这一点,我必须创建
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);
最终结果
...
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);
...
如果有人有更好的解决方案或答案来解释我为什么要这样做,我会在周一给你打分
为了实现这一点,我必须创建
txtE
,作为构造函数中初始化的静态变量。那在我的灵魂里一点也不舒服。因此,如果有人有更好的想法,一定要告诉别人。首先关闭对话框,然后设置txtE.requestFocus();只需使用RequestFocus()
。尝试移动它,但无效。请先关闭对话框,然后设置txtE.RequestFocus();只需使用RequestFocus()
。尝试将其移动,但无效。