如何使用DatePickerDialog从DialogFragment中设置文本

时间:2016-07-27 03:31:17

标签: java android textview android-dialogfragment

我正在构建一个应用程序,您可以在其中选择DatePickerDialog的日期,在您选择并点按"确定"后,您选择的日期必须显示在TextView中。

我遇到了这样的问题,因为我不能使用" setText.mTexView(foo)"在DialogFragment中...我已经尝试通过构造函数传递textview或者在片段中获取电视但是我无法帮助。

这是我的代码:

public class PickDate_main extends AppCompatActivity {

EditText tv_fecha;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pickdate_main);
}


public void showDatePickerDialog(View v){//This method is executed from the layout with a button

    PickDate_Dialog dialog = new PickDate_Dialog();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    dialog.show(ft, "Date pickeerr");
}
}

-

public class PickDate_Dialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{

public PickDate_Dialog(){
}

public Dialog onCreateDialog(Bundle savedInstanceState){

    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);
    return new PickDate_Dialog(getActivity(),this,year,month,day);
}

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


    String date = day + "-"+(month+1)+"-"+year;

    //I want to write the string "data", i suppose it must be here
}

}

1 个答案:

答案 0 :(得分:0)

我相信如果您将textview发送到PickDate_Dialog,保存并在必要时调用它,它应该可以工作。但不确定return new PickDate_Dialog(getActivity(),this,year,month,day);

public class PickDate_Dialog extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    private EditText dateEditText;

    public PickDate_Dialog(EditText et){
        dateEditText = et;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState){

        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);

        //Do you have a constructor for this? This is likely your issue

        return new PickDate_Dialog(getActivity(),this,year,month,day);
    }

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


        String date = day + "-"+(month+1)+"-"+year;
        if (dateEditText != null)
            dateEditText.setText(date);
    }

}

但是,这里有一些想法:

选项1:查看界面。这是一种允许您在类之间发送回调的方法(DatePickerDialog.OnDateSetListener就是一个例子)。在Dialog类中创建一个新接口

public interface PickDateInterface {
    void onDateSet(String date)
}

为它创建一个变量

私人PickDateInterface mPDInterface;

并将其添加到原始的onDateSet

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
    String date = day + "-"+(month+1)+"-"+year;

    if (mPDInterface != null) {
        mPDInterface.onDateSet(date);
    }
}

当您创建对话框时,只需将此界面添加为参数,并在界面中添加您需要执行的操作。



选项2:

在您的活动中创建一个公共方法并通过它调用它 (([你的活动名称])[你的背景])。你的方法; 您基本上是将您的上下文投射到您的活动并使用该方法 例: ((PickDate_main).getContext()).setDateText(date);