Android 使用Activity';创建可重用对话框的最佳方法是什么;生命周期?
我想建造Android 使用Activity';创建可重用对话框的最佳方法是什么;生命周期?,android,android-activity,dialog,dialogfragment,Android,Android Activity,Dialog,Dialogfragment,我想建造 可重用的对话框 完全可自定义的布局(自定义颜色或字体等) 使用引用的活动维护其生命周期 使用重载,我可以创建多种对话框变体,即标题、消息和回调的组合 以简单的方式(如果可能) 当前,我的自定义对话框类如下所示: public class CustomAlert { public interface OnSingleClickedListener { public void onPositiveClicked(); } public interface OnDualCli
- 可重用的对话框
- 完全可自定义的布局(自定义颜色或字体等)
- 使用引用的活动维护其生命周期
- 使用重载,我可以创建多种对话框变体,即标题、消息和回调的组合
- 以简单的方式(如果可能)
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..
}
我将这些活动中的警报称为:
if(!isFinishing()) {
CustomAlert.showAlert(MainActivity.this, getResources().getString(R.string.network_no_connection));
}
尽管我正在调用isFinishing()
检查主机活动是否正在运行,但我一直看到BadTokenException
,您的活动正在运行吗?
,我想可能isFinishing()
还不够
我发现这是使用<代码>对话片段< />代码,但是当我考虑上面的要求时,我觉得这是一个相当小的代码。
解决这个问题最推荐和有效的解决方案是什么提前谢谢 像这样创建一个自定义类,我在名为on play store的应用程序中创建了一个“竖起拇指”和“竖起拇指”对话框
//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>
使用getApplication context可避免此异常。当您的活动不易使用或被破坏,并且您尝试使用其上下文时,就会发生这种情况DialogFragments@Arslan我对使用ApplicationContext有点谨慎,因为可能会出现内存泄漏。我不确定这是否是最好的解决方案。@davidjons是的,我倾向于使用
DialogFragment
构建所有这些。谢谢你的评论!谢谢你花时间回答这个问题,但这正是我现在正在做的,我正在寻找一个更好的解决方案。
showRecommendDialog(this);