首页 > 解决方案 > 如何使用 setPositivebutton

问题描述

如何将 setPositivebutton 设置为另一个活动?

大家好,我正在为此使用android studio。我不明白如何使用让此按钮访问另一个活动。有谁知道如何纠正它?

 private void checkoutConfirmation() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Checkout Confirmation");
    builder.setMessage("Are you sure continue to checkout?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            global.clearCart();
            mAdapter.notifyDataSetChanged();
            Snackbar.make(view, "Checkout success", Snackbar.LENGTH_SHORT).show();

            @Override
            public void onClick(dialogInterface builder, int Button) {
<!--the error of the code-->
                Intent intent = new Intent(CartFragment.this, Main2Activity.class);
                startActivity(intent);

            }
        }
    });
    builder.setNegativeButton("No", null);
    builder.show();
}

标签: android

解决方案


如果我对您的理解正确,那么您想在单击“肯定”按钮时启动 MainActivity。这可以使用以下方法完成:

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        global.clearCart();
        mAdapter.notifyDataSetChanged();
        Snackbar.make(view, "Checkout success", Snackbar.LENGTH_SHORT).show();

            //Change CartFragment.this to getActivity() so that you can get the Activity Context here
            Intent intent = new Intent(getActivity(), Main2Activity.class);
            startActivity(intent);


    }
});

推荐阅读