java - 警报对话框 - 如何实现“取消”和“确定”按钮

标签 java android onclicklistener android-alertdialog

我有一个警报对话框,其中有两个按钮(确定和取消)。我想知道如何实现它。

因此,当您单击取消按钮时,它应该关闭警报对话框并返回到我当前所在的 fragment 。如果我单击“确定”按钮,它应该替换当前的警报对话框并将其替换为另一个警报对话框。

这是我下面的确认代码。 java 文件;

public class confirmation extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inf = getActivity().getLayoutInflater();
    final View theDIalog = inf.inflate(R.layout.example_xml, null);
    builder.setView(theDIalog);

AlertDialog dialog = builder.create();



    theDIalog.findViewById(R.id.makeaTransferOk).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Okay button is clicked", Toast.LENGTH_SHORT).show();
        }
    });


    theDIalog.findViewById(R.id.makeaTransferCancel).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    return dialog;
}
}

这是我的 example_xml 代码;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffc0c0c0">

<Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"
                android:id="@+id/makeaTransferCancel"/>

<Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK"
                android:id="@+id/makeaTransferOk"/>

</RelativeLayout>

请有人帮助我

最佳答案

尝试使用此代码来实现上面提到的功能:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
            //put your code that needed to be executed when okay is clicked
            dialog.cancel();


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

            AlertDialog alert11 = builder1.create();
            alert11.show();

关于java - 警报对话框 - 如何实现“取消”和“确定”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083115/

相关文章:

java - 列结果不符合预期

java - 应用程序中的 gradle 构建错误

java - OutOfMemoryErrors 即使​​在对键和值使用 Wea​​kReference 之后

android - 一些 Robolectric 测试在一起运行时失败但单独通过

java - 在servlet Filter的方法doFilter中没有调用

android - 是否可以仅使用 WonderPush REST API 向 Android 设备发送推送通知而不使用其 SDK?

android - 自动化浏览器和 native 应用程序

android - ExpandableListView 子点击监听器未触发

javascript - 更改dimple.js 中事件处理程序的行为?

java - Android onclicklistener 在不同的布局下,我一直失败