android将数据从Activity发送到DialogFragment

时间:2014-09-11 18:03:10

标签: android android-edittext alarm timepicker dialogfragment

我正在尝试创建一个根据时间戳设置闹钟的应用程序,我已经这样做但我希望将mainactivity中的edittext放入警报对话框中,该对话框会在alrm关闭时显示。

我想将文本从mainactivity发送到alertdemo。

MainActivity.class

private void stalarm() {
    View.OnClickListener setClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
            Intent i = new Intent("app.com.timepicker.demoactivity");

            /** Creating a Pending Intent */
            PendingIntent operation = PendingIntent.getActivity(getBaseContext(), unique, i, PendingIntent.FLAG_CANCEL_CURRENT);

            /** Getting a reference to the System Service ALARM_SERVICE */
            AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);

            /** Getting a reference to TimePicker object available in the MainActivity */
            TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);

            Calendar c = Calendar.getInstance();

            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            int hour = tpTime.getCurrentHour();
            int minute = tpTime.getCurrentMinute();

            /** Creating a calendar object corresponding to the date and time set by the user */
            GregorianCalendar calendar = new GregorianCalendar(year, month, day, hour, minute);

            /** Converting the date and time in to milliseconds elapsed since epoch */
            long alarm_time = calendar.getTimeInMillis();

            /** Setting an alarm, which invokes the operation at alart_time */
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);

            /** Alert is set successfully */
            Toast.makeText(getBaseContext(), "Alarm is set successfully", Toast.LENGTH_SHORT).show();
        }
    };

    Button btnSetAlarm = (Button) findViewById(R.id.btn_set_alarm);
    btnSetAlarm.setOnClickListener(setClickListener);

}

demoactivity.class

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window. This will be opened when the alarm goes off */
    alert.show(getSupportFragmentManager(), "AlertDemo");

}

alertdemo.class

public class AlertDemo extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
    getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

    /** Creating a alert dialog builder */
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /** Setting title for the alert dialog */
    builder.setTitle("alarm " );

    /** Setting the content for the alert dialog */
    builder.setMessage("An Alarm by AlarmManager");

    /** Defining an OK button event listener */
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            /** Exit application on click OK */
            getActivity().finish();
        }
    });

    /** Creating the alert dialog window */
    return builder.create();
}

/** The application should be exit, if the user presses the back button */
@Override
public void onDestroy() {
    super.onDestroy();
    getActivity().finish();
}

}

1 个答案:

答案 0 :(得分:1)

如果您要执行的操作是将EditText的内容发送到DialogFragment以对该信息执行某些操作,则可以使用Fragments的setArguments()功能。 This question涵盖了这样做的良好模式。

最终,您需要使用String参数为DialogFragment创建newInstance。该参数放在newInstance中的片段参数包中,并在片段生命周期的后期需要时访问。

当您致电newInstance时,只需执行DialogFragment.newInstance(editText.getText()即可获取要传递给片段管理器的DialogFragment。