2016-05-14 47 views
0

我有一个活动,打开包含DatePickerDialogFragment。我如何将选定日期返回到我的活动以及它如何在TextView?我查看了Android文档中的例子,但他们根本不清楚。我是编程新手。我找到的所有教程仅显示如何在DialogFragment内的祝酒中显示日期。如何从DialogFragment获取日期回到容器活动?

这是我DialogFrament代码

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ 

DatePickerFragmentListener datePickerListener; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    final Calendar c = Calendar.getInstance(); 

    //this makes current date as the default date of the calander 
    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 interface DatePickerFragmentListener { 
    void onDateSet(DatePicker view, int year, int month, int day); 
} 

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

    // what do i put here? 
} 
} 

?我把什么onDateSet方法中对我的活动?

public class FilterGuideList extends AppCompatActivity implements DatePickerFragment.DatePickerFragmentListener { 

private LinearLayout from_date; 
private LinearLayout to_date; 
private TextView from_text; 
private TextView to_text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_filter_guide_list); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    //initializing linearlayouts for date picker clickevent 
    from_date = (LinearLayout) findViewById(R.id.from_date_selector); 
    to_date = (LinearLayout) findViewById(R.id.to_date_selector); 

    from_text = (TextView) findViewById(R.id.from_date_text); 
    to_text = (TextView) findViewById(R.id.to_date_text); 


    from_date.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      DialogFragment newFragment = new DatePickerFragment(); 
      newFragment.show(getFragmentManager(), "datePicker"); 

     } 
    }); 

    to_date.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      DialogFragment newFragment = new DatePickerFragment(); 
      newFragment.show(getFragmentManager(), "datePicker"); 

     } 
    }); 
} 


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

     // what do i put here? 

} 
} 

我一直试图让这个工作2天。任何帮助将非常感激。

+0

考虑使用接口 – Shubhank

+0

内部oncreatedialog()创建对话框类型的变量,并设置onclicklistener它。 –

+0

我会推荐[EventBus](https://github.com/greenrobot/EventBus)来发送任何类型的数据:) – Vucko

回答

0

https://github.com/wdullaer/MaterialDateTimePicker 在上面的库中使用datePicker。

enter image description here

dependencies { 
    compile 'com.wdullaer:materialdatetimepicker:2.3.0' 
} 

使用所提供的工厂

Calendar now = Calendar.getInstance(); 
DatePickerDialog dpd = DatePickerDialog.newInstance(
    MainActivity.this, 
    now.get(Calendar.YEAR), 
    now.get(Calendar.MONTH), 
    now.get(Calendar.DAY_OF_MONTH) 
); 
dpd.show(getFragmentManager(), "Datepickerdialog"); 

实现一个OnDateSetListener

@Override 
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { 
    String date = "You picked the following date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year; 
    dateTextView.setText(date); 
} 

创建DatePickerDialog演示和细节请访问here

0
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ 

DatePickerFragmentListener datePickerListener; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

final Calendar c = Calendar.getInstance(); 

//this makes current date as the default date of the calander 
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 interface DatePickerFragmentListener { 
void onDateSet(DatePicker view, int year, int month, int day); 
} 

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

} 

您的活动

public class FilterGuideList extends AppCompatActivity implements DatePickerFragment.DatePickerFragmentListener { 

    private LinearLayout from_date; 
    private LinearLayout to_date; 
    private TextView from_text; 
    private TextView to_text; 

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

    //now you will get value of view,year,month and day here , you can put log here 

    }