0

我正在尝试制作一个根据时间选择器设置警报的应用程序,我已经这样做了,但我希望将 mainactivity 中的编辑文本放入警报对话框中,该对话框在警报响起时显示。

我想将文本从 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();
}

}
4

1 回答 1

1

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

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

当您调用您的newInstance简单操作DialogFragment.newInstance(editText.getText()以获取要传递给片段管理器的 DialogFragment 时。

于 2014-09-11T18:08:39.707 回答