java – Android:如何从日期和时间选择器获取值

前端之家收集整理的这篇文章主要介绍了java – Android:如何从日期和时间选择器获取值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个应用程序,我试图让用户选择时间,所以我使用了datePicker和timePicker以及一个按钮集.所以,当他点击设置按钮时,会出现一个对话框,说你选择了x日期和x时间,或者以任何方式在屏幕上显示消息,就像用户选择了这一次一样.所以我构建了代码,但随着我的应用程序进入该活动,它总是被强制停止.与显示数据相关的xml文件没有问题,因为当我评论从datePicker和timePicker获取值的 java行时,app绝对有效精细.
我仍然发布两个文件代码,以便于理解.还发布了日志猫异常.我删除了所有不必要的代码,如导入和包以及额外的按钮等,以使其快速可读.

这是.xml文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:weightSum="10"
  5. android:id="@+id/commonlayout" android:background="#FFFFFF">
  6.  
  7. <LinearLayout android:id="@+id/llheader"
  8. android:layout_width="fill_parent" android:layout_height="20dp"
  9. android:layout_weight="1"
  10. android:background="@drawable/bg">
  11.  
  12. <RelativeLayout android:id="@+id/relativeLayout1"
  13. android:layout_width="fill_parent" android:layout_height="fill_parent"
  14. android:layout_gravity="center"> <!--header-->
  15. <TextView
  16. android:id="@+id/txt_header"
  17. android:layout_width="wrap_content"
  18. android:layout_centerHorizontal="true"
  19. android:layout_centerVertical="true"/>
  20. </RelativeLayout>
  21. </LinearLayout>
  22. <ScrollView
  23. android:id="@+id/scrollView1"
  24. android:layout_width="wrap_content" android:layout_height="wrap_content"
  25. xmlns:android="http://schemas.android.com/apk/res/android">
  26. <LinearLayout android:layout_width="wrap_content"
  27. android:id="@+id/linearLayout1" android:orientation="vertical"
  28. android:layout_height="wrap_content"
  29. <DatePicker android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:id="@+id/datePicker"></DatePicker>
  32. <TimePicker android:id="@+id/timePicker"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content</TimePicker>
  35. <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content"
  36. android:layout_width="match_parent" android:gravity="center">
  37.  
  38. <Button android:id="@+id/btnSetDateTime"
  39. android:layout_height="wrap_content" android:text="Set"
  40. android:layout_width="wrap_content"/>
  41. <Button
  42. android:layout_width="fill_parent"
  43. android:layout_height="wrap_content"
  44. android:text="Next"
  45. android:id="@+id/btnNext1" />
  46. </TableRow>
  47. </LinearLayout>
  48. </ScrollView>
  49. <LinearLayout android:id="@+id/lldata"
  50. android:layout_weight="8" android:layout_width="fill_parent"
  51. android:layout_height="0dp" android:background="#FFFFFF">
  52. </LinearLayout>
  53.  
  54. <LinearLayout android:id="@+id/llfooter"
  55. android:layout_width="fill_parent"
  56. android:orientation="horizontal" android:layout_height="20dp"
  57. android:visibility="visible"
  58. android:layout_margin="0dp">
  59. </LinearLayout>

这是TimeDate.java的代码

  1. public class TimeDate extends Activity
  2. {
  3.  
  4.  
  5. Button btnNext;
  6. private TextView dateText;
  7. public void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.time_date);
  11. btnNext=(Button)this.findViewById(R.id.btnNext1);
  12. btnNext.setOnClickListener(new OnClickListener() {
  13. @Override
  14. public void onClick(View v)
  15. {
  16.  
  17. Intent myintent = new Intent(TimeDate.this,Next.class);
  18. startActivity(myintent);
  19. finish();
  20. }
  21. });
  22. ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date,null);
  23.  
  24. Button btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime);
  25. final DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker);
  26. final TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker);
  27.  
  28. btnSet.setOnClickListener(new OnClickListener() {
  29.  
  30. @Override
  31. public void onClick(View v) {
  32. String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();}});
  33.  
  34. AlertDialog alert = new AlertDialog.Builder(TimeDate.this).create();
  35. alert.setTitle("Reminder");
  36. alert.setView(DTPicker);
  37. alert.show();
  38.  
  39. }
  40. }

这是logcat异常:

  1. 04-27 11:48:24.830: D/AndroidRuntime(812): Shutting down VM
  2. 04-27 11:48:24.830: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
  3. 04-27 11:48:24.851: E/AndroidRuntime(812): FATAL EXCEPTION: main
  4. 04-27 11:48:24.851: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ogtpl.android/com.ogtpl.android.TimeDate}: java.lang.ClassCastException: android.widget.LinearLayout

解决方法

不要做任何额外的事情,你的代码就是完美的.只需删除ScrollLayout的代码或其属性,无论您在哪里使用它.
Lemme从ScrollLayout为您编写更新的代码
  1. final DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
  2. final TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
  3. btnSet.setOnClickListener(new OnClickListener() {
  4.  
  5. @Override
  6. public void onClick(View v) {
  7. String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();
  8.  
  9. Toast.makeText(TimeDate.this,"User selected " + strDateTime + "Time",Toast.LENGTH_LONG).show(); //Generate a toast only if you want
  10. finish(); // If you want to continue on that TimeDateActivity
  11. // If you want to go to new activity that code you can also write here
  12. }});

猜你在找的Android相关文章