我最初遵循关于android 开发人员的教程,但由于我计划在页面上各有 2 个,我认为从选择器中提取值会更优雅一些。我首先在 XML 中使用了 onClick 方法;但是,现在,一旦按下按钮(btn_schedule_adventure),我就会尝试提取这些值。尝试设置 DateView 时出现错误
DatePicker startDatePicker = (DatePicker) findViewById(R.id.picker_start_date);
我在下面的代码会引发错误。我如何: 1-从日期/时间选择器中提取值(绕过下面的错误) 2-同时保持默认时间/日期
提前致谢。
错误:
05-26 04:27:44.084: E/AndroidRuntime(798): FATAL EXCEPTION: main
05-26 04:27:44.084: E/AndroidRuntime(798): java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.DatePicker
05-26 04:27:44.084: E/AndroidRuntime(798): at com.scarr025.zwilio.AdventureSetUp$1.onClick(AdventureSetUp.java:40)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.view.View.performClick(View.java:4204)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.view.View$PerformClick.run(View.java:17355)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.os.Handler.handleCallback(Handler.java:725)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:137)
05-26 04:27:44.084: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-26 04:27:44.084: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 04:27:44.084: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 04:27:44.084: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-26 04:27:44.084: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-26 04:27:44.084: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method)
方法
public class AdventureSetUp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
Log.d("$$LOGCHECK$$", "In onCreate");
setContentView(R.layout.activity_adventure_set_up);
Button btnScheduleAdventure = (Button) findViewById(R.id.btn_schedule_adventure);
btnScheduleAdventure.setOnClickListener(new View.OnClickListener() {
//perform your action here
public void onClick(View v) {
// Get adventure title
EditText btnScheduleAdventure = (EditText) findViewById(R.id.adventure_title);
String adventureTitle = btnScheduleAdventure.getText().toString();
// Get adventure start date
DatePicker startDatePicker = (DatePicker) findViewById(R.id.picker_start_date);
int startDay = startDatePicker.getDayOfMonth();
int startMonth = startDatePicker.getMonth();
int startYear = startDatePicker.getYear();
Log.d("$$LOGCHECK$$", "Clicked Schedule Adventure");
Log.d("$$LOGCHECK$$", "Title: " + adventureTitle);
Log.d("$$LOGCHECK$$", "Start Date: " + startMonth + "-" + startDay + "-" + startYear);
Intent newIntent = new Intent();
}
});
}
日期/时间选择器代码(来自教程)
//---------------Date/Time Picker Methods-------------------------------------
// Date Picker
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) {
// Do something with the date chosen by the user
Log.d("$$LOGCHECK$$", "Date Picked--Year:" + year + " Month: " + month + " Day: " + day);
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
// Time Picker
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
XML
<LinearLayout 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"
android:orientation="vertical"
tools:context=".AdventureSetUp" >
<EditText
android:id="@+id/adventure_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint_adventure_title"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/picker_start_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_adventure_start_date"
android:onClick="showDatePickerDialog" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_adventure_start_time"
android:onClick="showTimePickerDialog" />
<Button
android:id="@+id/picker_end_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_adventure_end_date"
android:onClick="showDatePickerDialog" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_adventure_end_time"
android:onClick="showTimePickerDialog" />
<Button
android:id="@+id/btn_schedule_adventure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_schedule_adventure"
android:onClick="scheduleAdventure" />
</LinearLayout>