android - 如何使用 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();
}
解决方案
如果我对您的理解正确,那么您想在单击“肯定”按钮时启动 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);
}
});
推荐阅读
- python-3.x - Pypi.org 从命令提示符 Windows 中被阻止。点安装不起作用
- typescript - 如何在打字稿中用两次计算小时和分钟
- elasticsearch - Elasticsearch 匹配多个值并且输出结果应该在相同的数组元素序列中并应用排序
- android-studio - 工具 Windows 运行和 logcat android studio 3.5.1 不显示操作和日志
- javascript - 每 5 秒更换一次图片和链接
- c++ - 如何从 C++ 中的文件验证用户登录?
- java - 在 Web Flux 中记录 POST 请求正文
- python - 如果数字太大,为什么csv会删除开头的0?你怎么解决这个问题?
- r - R:计算两个最大值之间有多少个值
- angular - Angular:链式双向属性绑定