问题
I have problem with writing date I selected in DatePicker with Dialog Fragment. I`m trying to do this using Android Annotations. Problem is, I have to implement behavior for setting this date to TextView on OnDateSet function in DatePickerFragment class.
How can I choose in which TextView, which @Click should write?
Code below:
@EActivity(R.layout.activity_main_search)public class SearchActivity extends FragmentActivity{
private int mFYear, mFMonth, mFDay, mTYear, mTMonth, mTDay;
(R.id.main_search_city)
EditText cityET;
@ViewById(R.id.main_search_price)
EditText priceET;
@ViewById(R.id.main_search_from)
TextView dateFromTV;
@ViewById(R.id.main_search_to)
TextView dateToTV;
public static class DatePickerFragment 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) {
}
}
@AfterViews
public void setCurrentdateonView(){
final Calendar c = Calendar.getInstance();
mFYear = c.get(Calendar.YEAR);
mFMonth = c.get(Calendar.MONTH)+1;
mFDay = c.get(Calendar.DAY_OF_MONTH);
dateFromTV.setText(new StringBuffer().append(mFDay).append("-").append(mFMonth).append("-").append(mFYear).append(""));
dateToTV.setText(new StringBuffer().append(mFDay).append("-").append(mFMonth).append("-").append(mFYear).append(""));
}
@App
HApplication mApp;
@Click(R.id.main_search_from_btn)
public void showFromDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
@Click(R.id.main_search_to_btn)
public void showToDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
@Click(R.id.main_search_btn)
public void openSearch(){
//SearchActivity_.intent(this).city(cityET.getText().toString()).start();
ListActivity_.intent(this).city(cityET.getText().toString()).price(priceET.getText().toString()).start();
}}
Functions that should realise that date writing are:
@Click(R.id.main_search_from_btn) public void showFromDatePickerDialog(View v)
it should write date to dateFromTV
and
@Click(R.id.main_search_to_btn) public void showToDatePickerDialog(View v)
it should write date to dateToTV
I SOLVED THE PROBLEM.
What I needed was flags, which were set on each button @Click and then in onDateSet case switch with flag number check. Maybe it will help someone, because it took me whole day to figure it out...
回答1:
The Click annotation is just for triggering the dialog. You should implement a callback method through listener interfaces described here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
EDIT: I implemented a version for myself, here is the code:
MainActivity.java:
package com.example.datepickerexample;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
import com.example.datepickerexample.DatePickerFragment.DatePickerDialogListener;
public class MainActivity extends FragmentActivity implements
DatePickerDialogListener {
TextView lbl_from, lbl_to, cpn_from, cpn_to;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cpn_from = (TextView) findViewById(R.id.cpn_from);
cpn_to = (TextView) findViewById(R.id.cpn_to);
lbl_from = (TextView) findViewById(R.id.lbl_from);
lbl_to = (TextView) findViewById(R.id.lbl_to);
}
public void showFromDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("isFromDate", true);
newFragment.setArguments(bundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void showToDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putBoolean("isFromDate", false);
newFragment.setArguments(bundle);
newFragment.show(getSupportFragmentManager(), "datePicker");
}
@Override
public void onDatePicked(DialogFragment dialog, Calendar c,
boolean isFromDate) {
String strdate = null;
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
if (c != null) {
strdate = sdf.format(c.getTime());
}
if (isFromDate) {
lbl_from.setText(strdate);
} else {
lbl_to.setText(strdate);
}
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/cpn_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="6dp"
android:text="@string/from_date" />
<TextView
android:id="@+id/lbl_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/cpn_from"
android:layout_toRightOf="@id/cpn_from" />
<Button
android:id="@+id/btn_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cpn_from"
android:onClick="showFromDatePickerDialog"
android:text="@string/pick_from_date" />
<TextView
android:id="@+id/cpn_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_from"
android:layout_margin="6dp"
android:text="@string/to_date" />
<TextView
android:id="@+id/lbl_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn_from"
android:layout_alignBaseline="@id/cpn_to"
android:layout_toRightOf="@id/cpn_to" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cpn_to"
android:onClick="showToDatePickerDialog"
android:text="@string/pick_to_date" />
</RelativeLayout>
DatePickerFragment.java:
package com.example.datepickerexample;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
public interface DatePickerDialogListener {
public void onDatePicked(DialogFragment dialog, Calendar c,
boolean isFromDate);
}
// Use this instance of the interface to deliver action events
DatePickerDialogListener mListener;
boolean isFromDate;
public static DatePickerFragment newInstance(boolean isFromDate) {
DatePickerFragment instance = new DatePickerFragment();
instance = new DatePickerFragment();
Bundle args = new Bundle();
args.putBoolean("isFromDate", isFromDate);
instance.setArguments(args);
return instance;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isFromDate = getArguments().getBoolean("isFromDate");
}
@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) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
mListener.onDatePicked(this, c, isFromDate);
}
// Override the Fragment.onAttach() method to instantiate the
// NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the
// host
mListener = (DatePickerDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
}
来源:https://stackoverflow.com/questions/16943848/setting-date-using-dialogfragment-and-android-annotations