自定义进度对话框在安卓系统中会隐藏软键盘

6

查询说明:我正在使用透明主题的自定义进度对话框。这很好用。我的问题是,当我的键盘打开时,如果显示自定义进度对话框,则会强制关闭我的键盘。然而,如果我使用默认进度对话框,则可以正常工作。

这里是默认的进度对话框: 如您所见,即使进度对话框出现后,键盘仍在那里。我想在自定义进度对话框中实现同样的效果。

这里是自定义进度对话框: 如您所见,当对话框显示时,键盘会消失。这很烦人,每次用户都必须打开键盘。

以下是我的代码:

public class CustomProgressDialog extends Dialog {
private ImageView imageView;
private Context mContext;

public CustomProgressDialog(Context context) {
    super(context, R.style.DialogTheme);
    mContext = context;
    getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    wlmp.gravity = Gravity.CENTER_HORIZONTAL;
    getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(true);
    //setOnCancelListener(null);
    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(200, 200);
    imageView = new ImageView(context);
    imageView.setBackgroundResource(R.drawable.custom_dialog);
    layout.addView(imageView, params);
    addContentView(layout, params);
}

@Override
public void show() {
    super.show();
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
}
}
 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/actionbar_gradient_endColor</item>
</style>

非常感谢您的帮助。

<EditText android:id="@+id/et_search_category" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/imgClearSearch" 
    android:background="@drawable/rounded_rdt_txt" 
    android:hint="Search here..." 
    android:layout_centerVertical="true" 
    android:singleLine="true" />

@YvetteColomb:我在清单中没有设置任何IME选项。至于EditText,这是它的代码:<EditText android:id="@+id/et_search_category" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/imgClearSearch" android:background="@drawable/rounded_rdt_txt" android:hint="在此搜索..." android:layout_centerVertical="true" android:singleLine="true" /> - Ronak Thakkar
不用担心,@YvetteColomb。感谢您宝贵的时间。 :) - Ronak Thakkar
尝试这个:CustomProgressDialog 扩展 ProgressDialog。 - Maddy
2个回答

6

最终,我想出了解决方法:

我发现默认的进度对话框是扩展AlertDialog类的。然而,在我的代码中,我扩展了Dialog类。

现在我改为扩展ProgressDialog而不是Dialog,这个方法非常有效。

以下是代码:

public class MyCustomProgressDialog extends ProgressDialog {
private ImageView imageView;

public MyCustomProgressDialog(Context context) {
    super(context, R.style.TransparentProgressDialog);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wlmp = getWindow().getAttributes();
    wlmp.gravity = Gravity.CENTER;
    getWindow().setAttributes(wlmp);
    setTitle(null);
    setCancelable(false);
    setOnCancelListener(null);

}

public MyCustomProgressDialog(Context context, int theme) {
    super(context, theme);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_custom_progress_dialog);

    imageView = (ImageView) findViewById(R.id.centerImage);
    imageView.setBackgroundResource(R.drawable.custom_dialog);

}

@Override
public void show() {
    super.show();
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
}
}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/centerImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_centerInParent="true"
    android:visibility="visible" />
</RelativeLayout>

style.xml:

<style name="TransparentProgressDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

如何调用?
ProgessDialog mProgressDialog = new MyCustomProgressDialog(this);
mProgressDialog.show(); //to show
mProgressDialog.dismiss(); //to dismiss

1

1.了解软键盘是否正在显示。

2.显示或隐藏软键盘。

请尝试此方法。

private Context context;
private boolean isShow = false;

// soft keyboard is show or not
private boolean isSoftShowing() {
    // screen height
    int screenHeight = getWindow().getDecorView().getHeight();
    Rect rect = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

    return screenHeight - rect.bottom - getSoftButtonsBarHeight() != 0;
}

private int getSoftButtonsBarHeight() {
    DisplayMetrics metrics = new DisplayMetrics();
    // screen height ,not include navigation
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int usableHeight = metrics.heightPixels;
    // real screen height
    ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    int realHeight = metrics.heightPixels;
    if (realHeight > usableHeight) {
        return realHeight - usableHeight;
    } else {
        return 0;
    }
}

@Override
public void show() {
    super.show();
    // hideSoftKeyboard
    if(isSoftShowing()){
        ((Activity)context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        isShow = true;
    }
    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();
    frameAnimation.start();
}

@Override
public void dismiss() {
    super.dismiss();
    if(isShow){
        ((Activity)context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}

这将在每次关闭后打开键盘。在整个项目中:( - Ronak Thakkar
我希望它保持原样。如果在进度对话框之前可见,则应保持可见。如果在显示进度对话框之前不可见,则应保持不可见。即在所有情况下都不变。 - Ronak Thakkar
我明白了,我会尝试另一种方式。 - KeLiuyue
你可以再检查一遍。 - KeLiuyue
嘿,我想出了解决方案,请检查我的答案。非常感谢你投入宝贵的时间。我非常感激你:) 谢谢! - Ronak Thakkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接