将取消按钮添加到自定义AlertDialog

时间:2011-07-28 04:17:47

标签: java android dialog onclick alertdialog

我只是一个美国人,而不是程序员,任何帮助/指导都表示赞赏。 我正在尝试为此代码添加“取消”按钮:

public class gobuuf extends Activity {
/** Called when the activity is first created. */

class CustomAlertDialog extends AlertDialog {
    public CustomAlertDialog(Context context) {
        super(context);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean ret = super.onKeyDown(keyCode, event);
        finish();
        return ret;
    }

    public void setCancel(int buttonNegative, String string, Object object) {
        // TODO Auto-generated method stub

    }
}

private CustomAlertDialog mDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (isExistSkin("com.gau.go.launcherex")) {
        startGOLauncher("com.gau.go.launcherex");
        finish();
        return;
    }

    mDialog = new CustomAlertDialog(this);
    mDialog.setTitle(R.string.dialogtitle);
    mDialog.setMessage(getResources().getString(R.string.dialogcontent));
    mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks), null);
    mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(R.string.dialogok),
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String marketuriString = "market://search?q=pname:com.gau.go.launcherex";
            Intent EMarketintent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketuriString));
            EMarketintent.setPackage("com.android.vending");
            EMarketintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketuriString));     
            try {
                startActivity(EMarketintent);
            } catch (ActivityNotFoundException e) {
                String link = "http://61.145.124.93/soft/3GHeart/com.gau.go.launcherex.apk";
                Uri browserUri = Uri.parse(link);
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, browserUri);
                browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    startActivity(browserIntent);
                } catch (ActivityNotFoundException e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            } catch (Exception e3) {
                // TODO: handle exception
                e3.printStackTrace();
            }
            finish();

        }
    });
    mDialog.show();
}


@Override
protected void onDestroy() {
    super.onDestroy();
    android.os.Process.killProcess(android.os.Process.myPid());
}

private boolean isExistSkin(String packageName) {
    try {
            createPackageContext(packageName,
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (NameNotFoundException e) {
            return false;
        }
    return true;
}

private void startGOLauncher(String packageName){
    PackageManager packageMgr = this.getPackageManager();
    Intent launchIntent = packageMgr.getLaunchIntentForPackage(packageName);
    if (null != launchIntent){
        try
        {
            this.startActivity(launchIntent);
        }
        catch(ActivityNotFoundException e)
        {

        }
    }
}
}

我添加了相应的字符串,但不知道还能做什么:添加一个onclicklistener,我知道我需要某种行为编码在某处finish();或.cancel的东西。再次感谢您的帮助。

我应该说,我一直在玩这个你看到的那个:

mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE,getResources().getString(R.string.dialognothanks), null);

3 个答案:

答案 0 :(得分:3)

我不确定这是否正是您所寻找的,但您可以做这样的事情...这个只是关闭对话框。

.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }

有关创建对话框的非常好的教程,我建议使用:http://developer.android.com/guide/topics/ui/dialogs.html。这应该非常有用。

答案 1 :(得分:0)

现在正在通过这样做来实现我想要的方式:

1)我删除了:

public void setCancel(int buttonNegative, String string, Object object) {
    // TODO Auto-generated method stub
}

mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks), null);

2)并补充说:

mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks),
            new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                                mDialog.cancel();
                                        finish();
                                }
                        });

之前的

 mDialog.show();

试用和错误FTW。也许我应该从头开始学习Java和Android,以便以后节省时间。

现在我必须学习如何使取消按钮不仅完成();但也开始另一项活动。

答案 2 :(得分:0)

我想通过关注此页面上的答案来了解如何开始活动: Android: How to start an Activity from an alert dialog

所以我的代码现在看起来像:

mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks),
            new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getBaseContext(), AboutActivityOverview.class);    
                        startActivity(i);
                                }
                        });