当前,我的主要活动显示一个任意时间,并按住一个按钮,当按下该按钮时,将d出一个“时间选择器”对话框片段.用户在“片段”中设置新时间后,关闭片段,我希望我的主要活动使用用户选择的新时间来更新它的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; }}
主要活动
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); }}
总结 以上是内存溢出为你收集整理的退出对话框片段后,如何在MainActivity中更新TextView?全部内容,希望文章能够帮你解决退出对话框片段后,如何在MainActivity中更新TextView?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)