尝试从 DatePicker DialogFragment 设置 EditText 的文本 [英] Trying to set text of EditText from DatePicker DialogFragment

查看:33
本文介绍了尝试从 DatePicker DialogFragment 设置 EditText 的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从我在网上找到的许多 android 日期选择器教程之一创建的日期选择器对话框中获取日期.一旦选定文本日期,我似乎在实际检索文本日期的某处出错.我有一个 DatePickerFragment 类,它是从嵌套在我的 MainActivity 活动类中的 CreateEvent 片段调用的.这是日期选择器:

I am simply trying to get the date from a datepicker dialog I created from one of the many android datepicker tutorials I found online. I seem to be going wrong somewhere in the actual retrieving of the text date once it is selected. I have a DatePickerFragment class, which is called from a CreateEvent fragment which is nested inside my MainActivity activity class. This is the DatePicker:

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    private EditText txtDate;
    @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) {
        txtDate = (EditText)view.findViewById(R.id.txtDate);
        txtDate.setText(day + " " + (month + 1) + " " + year);
    }
}

当我实际尝试设置日期时,我得到 Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference意味着它实际上无法在我的创建事件片段 xml 文件中找到 edittext 元素.

I get Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence) on a null object reference when I actually try and set the date, which I understand means it can't actually find the edittext element in my create event fragment xml file.

我应该如何从 onDateSet 设置这个 edittext 元素的文本,或者我是否必须改变我的方法?

How should I go about setting the text of this edittext element from onDateSet or do I have to change my approach?

我尝试了这种方式,但无济于事.

I tried it this way but to no avail.

推荐答案

您需要使用接口datepicker获取数据到调用者片段:

You need to use an interface to get the data from the datepicker to the caller fragment:

public interface DateListener {
    void passDate(String date);
}

创建一个名为mListener成员变量:

private DateListener mListener;

覆盖 onAttach &onDetach fragment 方法:

Override the onAttach & onDetach fragment methods:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (DateListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement DateListener");
    }
}

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

接下来,在调用者fragment中实现这个interface并覆盖passDate方法:

Next, implement this interface in the caller fragment and override the passDate method:

@Override
public void passDate(String date) {
    // Do something with 'date', yourEditText.setText(date) for the example
}

你应该没事了.

这篇关于尝试从 DatePicker DialogFragment 设置 EditText 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆