我想建立..
目前我的自定义对话框类看起来像:
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()
来检查主机活动是否正在运行,我仍然看到BadTokenException
,is your Activity running?
,我想也许isFinishing()
是还不够。
我发现这个article正在使用DialogFragment
,但在我考虑上述要求时,我觉得这是一个很小的代码。
解决此问题的最佳推荐和有效解决方案是什么?
提前致谢!
答案 0 :(得分:0)
创建一个这样的自定义类,我已经在我的应用程序中名为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);