繁体   English   中英

如何在带单选按钮的警报对话框中添加确定和取消按钮

[英]How to add Ok and cancel button in alert dialog with radio button

我有一个片段(片段)和一个对话框片段(RadioListAlert)。 我想在警报对话框中添加“确定”和“取消”按钮。 当单击“确定”按钮时,该可选项目名称将传递给CarFragment。 怎么做。

我的代码在这里:

RadioListAlert.java:

 package com.h2o; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast; public class RadioListAlert extends DialogFragment { CharSequence[] tag = { "BMW", "AUDI", "MERCEDES", "FERRARI", "SKODA" }; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setCancelable(true); builder.setTitle("Tag Your Car").setSingleChoiceItems(tag, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getActivity(), tag[which], Toast.LENGTH_SHORT).show(); } }); return builder.create(); } } 

CarFragment.java:

 //Call RadioListAlert class new RadioListAlert().show(getActivity().getFragmentManager(), "Radio Alert"); 

请任何人帮助我!!!

提前致谢

您可以设置一个正面和负面的按钮:

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //your code to pass a bundle to fragment
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do what you want when cancel is clicked
                }
            });

您需要使用setNegativeButton()setOnPositiveButton方法添加确定和取消按钮。 要将数据发送到片段,您将需要创建一个接口来保存该数据,然后您的片段将实现它。 它将类似于new RadioListAlert().setDialogListener(CarFragment.this).show(getActivity().getFragmentManager(), "Radio Alert");

请记住,例如在关闭对话框之前,在为onClickListener Ok调用onClickListener时调用该侦听器。

编辑

public class RadioListAlert extends DialogFragment {

CharSequence[] tag = { "BMW", "AUDI", "MERCEDES", "FERRARI", "SKODA" };
private CarListener carListener;
private String itemClicked;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle("Tag Your Car").setSingleChoiceItems(tag, -1,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    Toast.makeText(getActivity(), tag[which],Toast.LENGTH_SHORT).show();
                    itemClicked = (String) tag[which];
                }
            }).setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
        {
            if(carListener != null)
                carListener.itemClicked(itemClicked);
            //to dismiss the dialog after user choose an item and click ok, you can also add some validation before dismissing the dialog
            dismiss();
        }
    });
    return builder.create();
}

public void setListener(CarListener carListener)
{
    this.carListener = carListener;
}


    public interface CarListener
    {
        void itemClicked(String text);
    }
}

在您的carFragment中实现CarListner

尝试这个

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    final View mView = inflater.inflate(R.layout.myalert, null);
    builder.setView(mView);

    final RadioGroup radioGroup = (RadioGroup) mView.findViewById(R.id.radioGroup1);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            int checkedId = radioGroup.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) mView.findViewById(checkedId);
            Log.d("alert", checkedId+"");

            --------------- your code---------------
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            --------------- your code---------------

        }
    });

    builder.show();

您的自定义警报对话框的布局是:myalert

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM