Android 退出对话框片段后,如何在MainActivity中更新文本视图?

Android 退出对话框片段后,如何在MainActivity中更新文本视图?,android,android-layout,android-fragments,android-view,android-timepicker,Android,Android Layout,Android Fragments,Android View,Android Timepicker,目前,我的主要活动显示一个任意时间,并按住一个按钮,当按下该按钮时,将显示一个时间选择器对话框片段。在用户在片段中设置新时间后,关闭片段,我希望我的主要活动使用用户选择的新时间更新其TextView 我现在的问题是,在用户设置时间后,我不知道如何更新活动中显示的时间。我尝试过玩onResume,但它一直导致应用程序崩溃。退出片段后刷新活动的最佳方式是什么 我试图实现的流程是,用户在片段中设置时间,导致共享首选项更新,然后主活动刷新,用所选的时间更新文本视图。我想得不对吗 这是我的TimePick

目前,我的主要活动显示一个任意时间,并按住一个按钮,当按下该按钮时,将显示一个时间选择器对话框片段。在用户在片段中设置新时间后,关闭片段,我希望我的主要活动使用用户选择的新时间更新其TextView

我现在的问题是,在用户设置时间后,我不知道如何更新活动中显示的时间。我尝试过玩onResume,但它一直导致应用程序崩溃。退出片段后刷新活动的最佳方式是什么

我试图实现的流程是,用户在片段中设置时间,导致共享首选项更新,然后主活动刷新,用所选的时间更新文本视图。我想得不对吗

这是我的TimePickerFragment类:

package com.example.habitabilitystudy;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.TimePicker;

public class TimePickerFragment extends DialogFragment implements
        TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
                "prediction", Context.MODE_PRIVATE);

        final Calendar c = Calendar.getInstance();
        int defaultHour = c.get(Calendar.HOUR_OF_DAY);
        int defaultMinute = c.get(Calendar.MINUTE);

        int hour = sharedPref.getInt("prediction_hour", defaultHour);
        int minute = sharedPref.getInt("prediction_min", defaultMinute);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
                "prediction", Context.MODE_PRIVATE);
        SharedPreferences.Editor editorh = sharedPref.edit();
        editorh.putInt("prediction_hour", hourOfDay);
        editorh.commit();
        SharedPreferences.Editor editorm = sharedPref.edit();
        editorm.putInt("prediction_min", minute);
        editorm.commit();
        mCallbacks.TimeUpdated();

    }

    /**
     * Interface
     */
    private FragmentCallbacks mCallbacks;

    public interface FragmentCallbacks {
        void TimeUpdated();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (FragmentCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    "Activity must implement Fragment Two.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

}
我的主要活动:

package com.example.habitabilitystudy;

import java.util.Calendar;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements
        TimePickerFragment.FragmentCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Use the current time as the default values for the picker
        SharedPreferences sharedPref = this.getSharedPreferences("prediction",
                Context.MODE_PRIVATE);

        final Calendar c = Calendar.getInstance();
        int defaultHour = c.get(Calendar.HOUR_OF_DAY);
        int defaultMinute = c.get(Calendar.MINUTE);

        int hour = sharedPref.getInt("prediction_hour", defaultHour);
        int minute = sharedPref.getInt("prediction_min", defaultMinute);

        String timeString = Integer.toString(hour) + ":"
                + Integer.toString(minute);

        TextView predictionText = (TextView) findViewById(R.id.prediction_time);
        predictionText.setText(timeString);
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    @Override
    public void TimeUpdated() {
        Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show();
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.predictButton:
            startActivity(new Intent(this, MainActivity.class));
            break;
        case R.id.login:
            startActivity(new Intent(this, LoginActivity.class));
            break;
        }
    }
}
我的主要活动xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/prediction_time"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="3dip"
        android:background="#5c755e"
        android:gravity="end"
        android:textColor="#3d4935"
        android:textSize="25sp"
        android:textStyle="italic"
        android:text="(c) appsrox.com" /> 


    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="@string/adjust_time" 
    android:onClick="showTimePickerDialog" />



</RelativeLayout>

您想在对话框片段中创建一个接口。选择时间后,它将通知活动。如果这给你带来任何问题,请告诉我

片段

public class TimePickerFragment extends DialogFragment implements
    TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
            "prediction", Context.MODE_PRIVATE);

        final Calendar c = Calendar.getInstance();
        int defaultHour = c.get(Calendar.HOUR_OF_DAY);
        int defaultMinute = c.get(Calendar.MINUTE);

        int hour = sharedPref.getInt("prediction_hour", defaultHour);
        int minute = sharedPref.getInt("prediction_min", defaultMinute);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
    }


    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Context context = getActivity();
        SharedPreferences sharedPref = context.getSharedPreferences(
            "prediction", Context.MODE_PRIVATE);
        SharedPreferences.Editor editorh = sharedPref.edit();
        editorh.putInt("prediction_hour", hourOfDay);
        editorh.commit();
        SharedPreferences.Editor editorm = sharedPref.edit();
        editorm.putInt("prediction_min", minute);
        editorm.commit();
        mCallbacks.TimeUpdated(hourOfDay, minute);
    }

    /**
     * Interface
     */
    private FragmentCallbacks mCallbacks;

    public interface FragmentCallbacks {
        void TimeUpdated(int hour, int minute);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (FragmentCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement Fragment Two.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

}
main活动

public class MainActivity extends Activity implements TimePickerFragment.FragmentCallbacks {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Use the current time as the default values for the picker
        SharedPreferences sharedPref = this.getSharedPreferences("prediction",
            Context.MODE_PRIVATE);

        final Calendar c = Calendar.getInstance();
        int defaultHour = c.get(Calendar.HOUR_OF_DAY);
        int defaultMinute = c.get(Calendar.MINUTE);

        int hour = sharedPref.getInt("prediction_hour", defaultHour);
        int minute = sharedPref.getInt("prediction_min", defaultMinute);

        String timeString = Integer.toString(hour) + ":"
            + Integer.toString(minute);

        TextView predictionText = (TextView) findViewById(R.id.prediction_time);
        predictionText.setText(timeString);
    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    @Override
    public void TimeUpdated(int hour, int minute) {
        Toast.makeText(this, "Hour: " + hour + " Minute: " + minute, Toast.LENGTH_SHORT).show();
        String timeString = Integer.toString(hour) + ":" + Integer.toString(minute);
        TextView predictionText = (TextView) findViewById(R.id.prediction_time);
        predictionText.setText(timeString);
    }
}

刚刚更新了我的答案。它应该向活动发送小时和分钟。试一下,让我知道它是否有效。使用OnDismissListener使用OnDismissListener非常感谢你帮了我的忙!然而,它仍然没有更新。设定好时间后,我收到了“更新”的闪光,但屏幕上仍然显示着旧的时间。对不起,在我完成输入之前,它保存了我的评论,哈哈。如果你看到我上面编辑的帖子,我仍然有问题,非常感谢你的帮助。@ConnorO'Doherty不用担心,我更新了我的答案。查看主活动时间更新(int-hour,int-minute)。@ConnorO'Doherty让我知道它是否更新了文本视图?不幸的是,我现在收到一个超时错误。有没有洞察到为什么会发生这种情况?它看起来应该工作得很好。我想我可能只需要重新启动我的模拟器