【问题标题】:What is the best way of creating a reusable dialog with Activity's lifecycle?使用 Activity 的生命周期创建可重用对话框的最佳方法是什么?
【发布时间】:2015-03-31 06:06:38
【问题描述】:

我想建造..

  • 可重复使用的对话框
  • 完全可自定义的布局(自定义颜色或字体等)
  • 使用引用的 Activity 维护其生命周期
  • 使用重载,我可以创建多种对话框变体,即标题、消息和回调的组合
  • 以简单的方式(如果可能)

目前我的自定义对话框类看起来像:

public class CustomAlert {

public interface OnSingleClickedListener {
    public void onPositiveClicked();
}

public interface OnDualClickedListener {
    public void onPositiveClicked();
    public void onNegativeClicked();
}

/**
 * Show simple alert without callback.
 * @param context
 * @param msg
 */
public static void showAlert(Context context, String msg) {
    final Dialog dialog = new Dialog(context);
    // Do some stuff

    ok.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    dialog.show();
}

/**
 * Show simple alert with callback.
 * @param context
 * @param msg
 * @param listener
 */
public static void showAlert(Context context, String msg, final OnSingleClickedListener listener) {
    final Dialog dialog = new Dialog(context);
    // Do some stuff

    ok.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View view) {
            dialog.dismiss();
            listener.onPositiveClicked();
        }
    });
    dialog.show();
}
// Some other methods..
}

我这样从 Activity 调用这些警报:

if(!isFinishing()) {
    CustomAlert.showAlert(MainActivity.this, getResources().getString(R.string.network_no_connection));
}

即使我正在调用 isFinishing() 来检查主机 Activity 是否正在运行,我仍然看到 BadTokenExceptionis your Activity running?,并且我认为 isFinishing() 可能还不够。

我发现这个article 正在使用DialogFragment,但是当我考虑到上述要求时,我觉得对于这样一个小任务来说这是相当多的代码。

解决这个问题最推荐和最有效的解决方案是什么?

提前致谢!

【问题讨论】:

  • 使用 getApplication 上下文来避免这个异常。当您的 Activity 使用或销毁时不可见并且您尝试使用其上下文时,就会发生这种情况
  • 推荐方式是DialogFragments
  • @Arslan 由于潜在的内存泄漏,我对使用 ApplicationContext 有点谨慎。我不确定这是否是最好的解决方案。
  • @DavidJhons 是的,我倾向于使用DialogFragment 构建所有这些。感谢您的评论!

标签: android android-activity dialog dialogfragment


【解决方案1】:

创建一个这样的自定义类,我在 Play 商店中名为 Debongo - Restaurant Finder 的应用程序中创建了这个对话框,用于竖起大拇指和大拇指向下

//recommend dialog
public void showRecommendDialog(Context mContext)
{
     dialog = new Dialog(mContext,android.R.style.Theme_Holo_Dialog_NoActionBar);
     dialog.setContentView(R.layout.recommend_dialog);
     dialog.show();

    btnThumbsUp = (ImageButton) dialog.findViewById(R.id.btn_Yes);
    btnThumbsDown = (ImageButton) dialog.findViewById(R.id.btn_no);
    txtMsg=(TextView) dialog.findViewById(R.id.txtMsg);

    ImageButton cancelRecommend=(ImageButton) dialog.findViewById(R.id.cancelRecommend);
    cancelRecommend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
            dialog=null;
        }
    });

    btnThumbsUp.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do what you want
        }
    });


    btnThumbsDown.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            //do what you want
        }
    });
}

这是相同的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:gravity="center"
android:padding="5dp" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageButton
        android:id="@+id/cancelRecommend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent"
        android:contentDescription="@null"
        android:src="@drawable/com_facebook_close" />

    <TextView
        android:id="@+id/txtMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cancelRecommend"
        android:layout_marginBottom="@dimen/viewSpace3"
        android:layout_marginLeft="@dimen/viewSpace3"
        android:layout_marginRight="@dimen/viewSpace3"
        android:text="Do You Want To Recommend This Restaurant ?"
        android:textColor="#000000"
        android:textSize="@dimen/titlebar_textSize"
        tools:ignore="HardcodedText" />

    <View
        android:layout_width="0dip"
        android:layout_height="@dimen/viewSpace3" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtMsg"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center" >

        <ImageButton
            android:id="@+id/btn_Yes"
            android:layout_width="@dimen/viewSpace5"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:contentDescription="@null"
            android:src="@drawable/unselected_thumbs_up" />

        <View
            android:id="@+id/vvv"
            android:layout_width="@dimen/viewSpace1"
            android:layout_height="@dimen/viewSpace3"
            android:layout_toRightOf="@id/btn_Yes" />

        <ImageButton
            android:id="@+id/btn_no"
            android:layout_width="@dimen/viewSpace5"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/vvv"
            android:background="@android:color/transparent"
            android:contentDescription="@null"
            android:src="@drawable/unselected_thumbs_down" />

    </RelativeLayout>

</RelativeLayout>

现在当你想打开这个对话框时,你可以直接调用它

showRecommendDialog(this);

【讨论】:

  • 感谢您花时间回答这个问题,但这正是我现在正在做的事情,我正在寻找更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2012-01-22
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
相关资源
最近更新 更多