Click 注释仅用于触发对话框。您应该通过此处描述的侦听器接口实现回调方法:http:
//developer.android.com/guide/topics/ui/dialogs.html#PassingEvents
编辑:我为自己实现了一个版本,这里是代码:
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);
}
}
}
活动主.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");
}
}
}