gpt4 book ai didi

Android:在 EditText 焦点上的自定义 AlertDialog 中显示软键盘

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:24 27 4
gpt4 key购买 nike

我有一个自定义的 AlertDialog,但是当您点击布局中的 EditText 字段时,软键盘不会自动出现。我试过这个解决方案 Android: EditText in Dialog doesn't pull up soft keyboard使用:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

这可能很简单,因为我没有将代码放在正确的位置。我在Activity的onCreateDialog和onPrepareDialog以及自定义AlertDialog的构造函数和onCreate中都试过了。那没有用。

我更喜欢这个解决方案,因为这似乎是最好的做法,而不是尝试为 EditText 字段设置一个 onFocus 监听器。

我是如何在 Activity 中尝试的

@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case LOCATION_DETAILS_DIALOG:
dialog = new LocationDetails(this, detailsSetListener);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
return dialog;

default:
return null;
}
}

protected void onPrepareDialog(final int id,final Dialog dialog){
super.onPrepareDialog(id, dialog);
switch (id) {
case LOCATION_DETAILS_DIALOG:
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
}

我是如何在 AlertDialog 类中尝试的

public LocationDetails(Context context, DetailsSetEventListener detailsSetEventListener) {
super(context);
this.context = context;
this.detailsSetEventListener = detailsSetEventListener;
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

知道为什么这不起作用吗?

最佳答案

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

对我来说很好,我把它放在构造函数中,比如

public CustomDialog(Context context) {
super(context);
show();
setContentView(R.layout.widget_custom_dialog);

getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

把AlertDialog改成Dialog会导致我的dialog位置错误,所以我就用这个方法。

关于Android:在 EditText 焦点上的自定义 AlertDialog 中显示软键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9262205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com