2016-05-14 3 views
0

私は、DatePickerを含むDialogFragmentを開くアクティビティを持っています。選択した日付を自分のアクティビティに戻すにはどうすればいいですか?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"); 

を用い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); 
} 

を実装デモや詳細については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 

    } 
関連する問題