Android 从 DialogFragment 类返回并显示 DatePicker

标签 android datepicker android-dialogfragment

我对 Android 编程有点陌生,所以请多多包涵...

我正在学习来自 Google Android - Pickers 的教程, 但在完成之后,我不知道如何返回并显示原始 Activity 中的选定日期。

如果现在听起来很困惑,我目前有 2 个类 - ClassA.javaClassB.java,其中 ClassA 是我的主要类 Activity ,ClassB 是我的对话 fragment 。

我知道下面这个函数(在 ClassB 中)接受 View 、年、月和文本并返回一些东西,但我不知道如何将其存储回 ClassA 的主函数中,并将其显示为 Spinner(TextView)中的当前选定值。

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    return [something];
}

我在 ClassA 中的函数在用户点击(onClick)Spinner(TextView) :

public void DatePicker(View v) {
    DialogFragment DateFragment = new NotifyDatePicker();
    DateFragment.show(getSupportFragmentManager(), "DatePicker");
}

所以问题是,我从哪里开始存储数据?我是否获取从 ClassBClassA 的返回(是否甚至返回值?)并将其存储到 Bundle 中(同时还更新 R.id.TextViewSpinner),还是在用户位于 DialogFragment< 中时保存它/em>(B 级)?

编辑

我刚刚意识到我的错误。 ClassB 是一个独立的 Activity ,因此 Spinner(TextView)的更改和 Bundle 的存储应该在 中完成B类...

不过还有一个小问题。

我正在使用 (TextView)findViewById(R.id.TextViewSpinner).setText(daymonthyear) 但错误不断出现:

The method findViewById(int) is undefined for the type ClassB

有没有我忘记导入/包含的东西?

完整代码

public class ClassB extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {

        // Change TextViewSpinner here (setText)

        // Tried (TextView)findViewById(R.id.TextViewSpinner).setText(daymonthyear) - The method findViewById(int) is undefined for the type ClassB error returned

        // Tried (TextView)context.findViewById(R.id.TextViewSpinner) - context cannot be resolved error returned

        return;
    }
}

这是通过 Google Tutorial .我一直在这里和那里进行调整,但它总是让我回到原点...

最佳答案

也许不是最好的解决方案,但您可以使用 DefaultSharedPreferences 来保存值:

public void onDateSet(DatePicker view, int year, int month, int day) {

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.putInt("year", year);
    preferences.putInt("month", month);
    preferences.putInt("day", day;
    preferences.commit();

    return;
}

并在 Activity A 中对它们做一些事情:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
int year = preferences.getInt("year", 0);
int month = preferences.getInt("month", 0);
int day = preferences.getInt("day", 0);

(TextView)findViewById(R.id.TextViewSpinner).setText(day + "/" + month + "/" + year);    

希望对您有所帮助。

顺便说一下,建议像 TextViewSpinner 这样的引用以非大写字母开头:textViewSpinner

关于Android 从 DialogFragment 类返回并显示 DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13045581/

相关文章:

android - 在我的 android 项目中使用 android box 2d

javascript - JqueryUI Datepicker无法正确处理初始值

jquery-ui - JQUERY datepicker日历,如何将 "done"按钮重命名为 "close"

android - DialogFragment 默认背景颜色更改 - Android

java - 在 list 中包含特定 java 文件时发生 fragment 错误

java - 将整个 C++ 应用程序包装到 java?

java - 将 Xml 布局到我的 Android 应用程序

javascript - 更改下拉选择选项取决于日期选择器选择

android - 显示 DialogFragments 会使 ICS 崩溃

android - 如何更改 snackbar 的背景颜色?