【问题标题】:android send data from Activity to DialogFragmentandroid将数据从Activity发送到DialogFragment
【发布时间】:2014-09-11 18:03:10
【问题描述】:

我正在尝试制作一个根据时间选择器设置警报的应用程序,我已经这样做了,但我希望将 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();
}

}

【问题讨论】:

    标签: android android-edittext alarm timepicker dialogfragment


    【解决方案1】:

    如果您想要将 EditText 的内容发送到 DialogFragment 以处理该信息,您可以使用 Fragments 的setArguments() 功能。 This question 涵盖了一个很好的模式。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多