AlertDialog - 显示带有自定义 class 的软键盘不起作用
AlertDialog - Show soft keyboard with a custom class not working
我的问题是我的自定义提示对话框 class 没有正确显示软键盘。我正在使用
创建它
SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();
软键盘不显示。我已经按照其他 Whosebug 答案显示键盘 ... Show soft keyboard for dialog
如果我不使用自定义 class
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(R.layout.alertdialog_settings);
AlertDialog alertDialog = mBuilder.create();
alertDialog.show();
然而,当使用自定义 AlertDialog 时 class 我似乎无法获得与上图相同的结果
我试过手动显示键盘
SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
但是它显示了警报对话框后面的键盘,并且没有提供与 AlertDialog Builder 相同的效果。
如何使用自定义 AlertDialog 显示软键盘,以便像使用 AlertDialog Builder 一样输出?
编辑:
我也试过在 AlertDialog 的 onCreate 方法中手动显示它
public class SettingsDialog extends AlertDialog {
public SettingsDialog(@NonNull Context context, String subName) {
super(context);
this.mContext = context;
this.mSubName = subName;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog_settings);
InputMethodManager imm = (InputMethodManager)
mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
}
但是这仍然会导致键盘显示在 alertDialog 后面
尝试延迟显示键盘,例如对话框创建后 100-200 毫秒,或者只是尝试使用 EditText.requestFocus() 请求对话框的 EditText 焦点,并在对话框创建后延迟。
我试过很多其他方法,但这个终于奏效了。
SortByDialog sortByDialog = new SortByDialog(MainActivity.this);
sortByDialog.show();
sortByDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
需要确保 clearFlags 位于自定义 AlertDialogClass
的 .show()
之后
我认为您不需要扩展警报对话框 class,您可以做的只是自定义 java,其中包含创建自定义对话框的辅助函数,因此您仍然可以代码抽象并可以轻松添加其他功能。
public class SettingsDialog {
private AlertDialog.Builder mBuilder = null;
private AlertDialog alertDialog = null;
public SettingsDialog(@NonNull Context context, String subName) {
this.mSubName = subName;
this.mContext = context;
}
public show(){
mBuilder = new AlertDialog.Builder(mContext);
mBuilder.setView(R.layout.someID);
alertDialog = mBuilder.create();
alertDialog.show();
}
public void dismiss(){
if(alertDialog == null) return;
alertDialog.dismiss();
}
// can use interface to handle callbacks
}
// usage
SettingsDialog sd = new SettingsDialog(this, "MATHS");
sd.show();
//sd.dismiss();
我的问题是我的自定义提示对话框 class 没有正确显示软键盘。我正在使用
创建它SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();
软键盘不显示。我已经按照其他 Whosebug 答案显示键盘 ... Show soft keyboard for dialog
如果我不使用自定义 class
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setView(R.layout.alertdialog_settings);
AlertDialog alertDialog = mBuilder.create();
alertDialog.show();
然而,当使用自定义 AlertDialog 时 class 我似乎无法获得与上图相同的结果
我试过手动显示键盘
SettingsDialog settingsDialog = new SettingsDialog(MainActivity.this);
settingsDialog.show();
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
但是它显示了警报对话框后面的键盘,并且没有提供与 AlertDialog Builder 相同的效果。
如何使用自定义 AlertDialog 显示软键盘,以便像使用 AlertDialog Builder 一样输出?
编辑:
我也试过在 AlertDialog 的 onCreate 方法中手动显示它
public class SettingsDialog extends AlertDialog {
public SettingsDialog(@NonNull Context context, String subName) {
super(context);
this.mContext = context;
this.mSubName = subName;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdialog_settings);
InputMethodManager imm = (InputMethodManager)
mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
}
}
但是这仍然会导致键盘显示在 alertDialog 后面
尝试延迟显示键盘,例如对话框创建后 100-200 毫秒,或者只是尝试使用 EditText.requestFocus() 请求对话框的 EditText 焦点,并在对话框创建后延迟。
我试过很多其他方法,但这个终于奏效了。
SortByDialog sortByDialog = new SortByDialog(MainActivity.this);
sortByDialog.show();
sortByDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
需要确保 clearFlags 位于自定义 AlertDialogClass
的.show()
之后
我认为您不需要扩展警报对话框 class,您可以做的只是自定义 java,其中包含创建自定义对话框的辅助函数,因此您仍然可以代码抽象并可以轻松添加其他功能。
public class SettingsDialog {
private AlertDialog.Builder mBuilder = null;
private AlertDialog alertDialog = null;
public SettingsDialog(@NonNull Context context, String subName) {
this.mSubName = subName;
this.mContext = context;
}
public show(){
mBuilder = new AlertDialog.Builder(mContext);
mBuilder.setView(R.layout.someID);
alertDialog = mBuilder.create();
alertDialog.show();
}
public void dismiss(){
if(alertDialog == null) return;
alertDialog.dismiss();
}
// can use interface to handle callbacks
}
// usage
SettingsDialog sd = new SettingsDialog(this, "MATHS");
sd.show();
//sd.dismiss();