在Dialog Fragment和Activity之间传递变量的有效方法?

时间:2015-01-19 16:47:04

标签: android dialog fragment

我的主要活动设置并开始一个对话框片段供用户与之交互。该对话框显示几个单选按钮可供选择,完成后可以取消或接受选择。主要活动使用接口在对话框片段被解除后检查事件,然后根据对话框片段中存在的相同变量更改局部变量。这对我来说似乎是多余的,那么有更好的方法来实现这种沟通吗?看看主要活动中的NOTI_TIMER。

主要活动:

    public void dialogSetup(){

    RadioButtonDialog dialog = new RadioButtonDialog();

    dialog.show(getFragmentManager(), "Radio Button Dialog");
}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {

    NOTI_TIMER = RadioButtonDialog.getNOTI();
    startAlarm();
}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {

    log("in onDialogNegativeClick");
}

对话片段:

public class RadioButtonDialog extends DialogFragment{

public String TAG = "RadioButtonDialog";
public static long NOTI_TIMER = 0;

RadioGroup mRadioGroup;
Button timerChoice;

/* The activity that creates an instance of this dialog fragment must
 * implement this interface in order to receive event callbacks.
 * Each method passes the DialogFragment in case the host needs to query it. */
public interface RadioButtonDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}

// Use this instance of the interface to deliver action events
RadioButtonDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (RadioButtonDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement NoticeDialogListener");
    }
}


@Override
public Dialog onCreateDialog(Bundle SavedInstanceState){

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.timer_choice_layout, null);
mRadioGroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
builder.setView(view);
builder.setTitle("Choose Selfie Timer Duration");
//timerChoice = (Button)view.findViewById(R.id.button1);

builder.setNegativeButton(R.string.delete_cancel, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

        Log.i(MainActivity.TAG, "User pressed 'Cancel'.");
        mListener.onDialogNegativeClick(RadioButtonDialog.this);

    }
});

builder.setPositiveButton(R.string.timer_btn_confirm, new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

        switch (mRadioGroup.getCheckedRadioButtonId()){

        case R.id.radioButton1:
            NOTI_TIMER = 10800000;
            Toast.makeText(getActivity(), R.string.three_hours, Toast.LENGTH_LONG).show();
            break;

        case R.id.radioButton2:
            Toast.makeText(getActivity(), R.string.six_hours, Toast.LENGTH_LONG).show();
            NOTI_TIMER = 21600000;
            break;

        case R.id.radioButton3:
            Toast.makeText(getActivity(), R.string.nine_hours, Toast.LENGTH_LONG).show();
            NOTI_TIMER = 32400000;
            break;

        case R.id.radioButton4:
            Toast.makeText(getActivity(), R.string.twelve_hours, Toast.LENGTH_LONG).show();
            NOTI_TIMER = 43200000;
            break;

        case R.id.radioButton5:
            Toast.makeText(getActivity(), R.string.twentyfour_hours, Toast.LENGTH_LONG).show();
            NOTI_TIMER = 86400000;
            break;

        }
        mListener.onDialogPositiveClick(RadioButtonDialog.this);
    }
});

Dialog dialog = builder.create();
return(dialog);
}

public static long getNOTI() {

    return NOTI_TIMER;
}

}

0 个答案:

没有答案