【问题标题】:Android: How to get values from date and time pickerAndroid:如何从日期和时间选择器中获取值
【发布时间】:2012-05-07 23:15:01
【问题描述】:

我正在构建一个应用程序,我试图让用户选择时间,所以我使用了datePickertimePickerbutton set。因此,当他点击set button 时,dialog box 会出现并说就像您选择了 x 日期和 x 时间,或者以任何方式,屏幕上应该出现一条消息,就像用户选择了这个时间一样。所以我构建了代码,但是当我的应用程序进入该活动时,它总是被强制停止。与显示数据相关的xml file 没有问题,因为当我评论从datePicker 和@987654328 获取值的java 行时@,应用程序工作得很好。 我仍然发布两个文件代码以便于理解。还发布了日志猫异常。我已经删除了所有不必要的代码,如导入和包以及额外的按钮等,以便快速阅读。

这是.xml文件的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="10"
android:id="@+id/commonlayout" android:background="#FFFFFF">

<LinearLayout android:id="@+id/llheader"
    android:layout_width="fill_parent" android:layout_height="20dp"
     android:layout_weight="1"
     android:background="@drawable/bg">

    <RelativeLayout android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_gravity="center"> <!--header-->
        <TextView
            android:id="@+id/txt_header"
            android:layout_width="wrap_content"          
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"/>            
        </RelativeLayout>           
</LinearLayout>
 <ScrollView 
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout1" android:orientation="vertical"
    android:layout_height="wrap_content" 
<DatePicker android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
  android:id="@+id/datePicker"></DatePicker>
<TimePicker android:id="@+id/timePicker"
    android:layout_width="wrap_content"   
  android:layout_height="wrap_content</TimePicker>
<TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content"
    android:layout_width="match_parent" android:gravity="center">

    <Button android:id="@+id/btnSetDateTime"
        android:layout_height="wrap_content" android:text="Set"
        android:layout_width="wrap_content"/>
     <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Next"                       
        android:id="@+id/btnNext1" />   
  </TableRow>
 </LinearLayout>
 </ScrollView>    
<LinearLayout android:id="@+id/lldata"
    android:layout_weight="8" android:layout_width="fill_parent"
    android:layout_height="0dp" android:background="#FFFFFF">           
</LinearLayout>

<LinearLayout android:id="@+id/llfooter"
    android:layout_width="fill_parent"
    android:orientation="horizontal" android:layout_height="20dp"
    android:visibility="visible" 
    android:layout_margin="0dp">     
</LinearLayout>

这是TimeDate.java的代码:

public class TimeDate extends Activity
{


Button  btnNext;
private TextView dateText;
 public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.time_date);
        btnNext=(Button)this.findViewById(R.id.btnNext1);           
        btnNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) 
            {

                    Intent myintent = new  Intent(TimeDate.this, Next.class);
                    startActivity(myintent);
                    finish();
                    }
        });
        ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null);

        Button  btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime);
        final   DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker);
        final   TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker);

        btnSet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();}});

       AlertDialog alert = new AlertDialog.Builder(TimeDate.this).create();
        alert.setTitle("Reminder");
        alert.setView(DTPicker);
        alert.show();

 }       
}

这是logcat 异常:

04-27 11:48:24.830: D/AndroidRuntime(812): Shutting down VM
04-27 11:48:24.830: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-27 11:48:24.851: E/AndroidRuntime(812): FATAL EXCEPTION: main
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

【问题讨论】:

  • 遇到同样的问题如何获得这些值..请有人回答我会为他/她投票:)

标签: java android date time datepicker


【解决方案1】:

不要做任何额外的事情,你的代码是完美的。只需删除ScrollLayout 的代码或其属性,无论您在哪里使用它。 让我从ScrollLayout为你写更新代码

 final   DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
        final   TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
        btnSet.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();

                Toast.makeText(TimeDate.this, "User selected " + strDateTime + "Time", Toast.LENGTH_LONG).show(); //Generate a toast only if you want 
                finish();   // If you want to continue on that TimeDateActivity
                 // If you want to go to new activity that code you can also write here
            }});

【讨论】:

  • GetCurrentHour 已被弃用。 6.0+ 现在使用 getHour()。
【解决方案2】:
ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null);
Button  btnSet = (Button) DTPicker.findViewById(R.id.btnSetDateTime);
final   DatePicker dp = (DatePicker) DTPicker.findViewById(R.id.datePicker);
final   TimePicker tp = (TimePicker) DTPicker.findViewById(R.id.timePicker);

将上面一行替换为下面

ScrollView DTPicker = (ScrollView)findViewById(R.id.scrollView1);
Button  btnSet = (Button)findViewById(R.id.btnSetDateTime);
final   DatePicker dp = (DatePicker)findViewById(R.id.datePicker);
final   TimePicker tp = (TimePicker)findViewById(R.id.timePicker);

【讨论】:

  • 对不起,伙计,但仍然无法正常工作。这是日志猫异常 W/dalvikvm(1007): threadid=1: thread exiting with uncaught exception (group=0x4001d800) E/AndroidRuntime(1007): FATAL EXCEPTION: main E/AndroidRuntime(1007): java.lang.RuntimeException :无法启动活动 ComponentInfo{com.ogtpl.android/com.ogtpl.android.TimeDate}:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。
  • 嘿,请尝试帮助男人,我会接受并支持您的回答。
  • 好吧,我的错误很抱歉,但我之前遇到过几个书呆子,他们只是为了声誉而来到这里。很抱歉,如果你能解决这个问题,我真的很感激你
  • 创建一个单独的滚动视图xml文件并通过inflater对其进行充气,然后将其设置为警报对话框
  • 那会很好用,我知道因为我已经这样做了,但我想在一个 xml 屏幕上执行此操作,所以有什么方法可以在一个屏幕上执行此操作
【解决方案3】:

您的问题似乎在这一行:

ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null);

您正在膨胀包含 LinearLayout 作为根节点的 XML,但是您将其类型转换为 ScrollView 导致 ClassCastException

【讨论】:

    【解决方案4】:

    试试下面的代码....

    time_date.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="10" android:id="@+id/commonlayout"
        android:background="#FFFFFF">
    
        <LinearLayout android:id="@+id/llheader" android:layout_width="fill_parent" android:layout_height="20dp"
            android:layout_weight="1">
            <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:layout_gravity="center"> <!--header -->
                <TextView android:id="@+id/txt_header" android:layout_width="wrap_content" android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true" android:layout_height="wrap_content" />
            </RelativeLayout>
        </LinearLayout>
    
        <ScrollView android:id="@+id/scrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <LinearLayout android:layout_width="wrap_content" android:id="@+id/linearLayout1" android:orientation="vertical"
                android:layout_height="wrap_content">
    
                <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datePicker" />
                <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    
                <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="match_parent"
                    android:gravity="center">
    
                    <Button android:id="@+id/btnSetDateTime" android:layout_height="wrap_content" android:text="Set"
                        android:layout_width="wrap_content" />
                    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Next"
                        android:id="@+id/btnNext1" />
                </TableRow>
    
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout android:id="@+id/lldata" android:layout_weight="8" android:layout_width="fill_parent"
            android:layout_height="0dp" android:background="#FFFFFF">
        </LinearLayout>
    
        <LinearLayout android:id="@+id/llfooter" android:layout_width="fill_parent" android:orientation="horizontal"
            android:layout_height="20dp" android:visibility="visible" android:layout_margin="0dp">
        </LinearLayout>
    
    </LinearLayout>
    

    DateTimeActivity.java

    public class DateTimeActivity extends Activity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.time_date);
            setTitle("Reminder");
    
            Button btnNext = (Button) findViewById(R.id.btnNext1);           
            btnNext.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent myintent = new  Intent(DateTimeActivity.this, Next.class);
                    startActivity(myintent);
                    finish();
                }
            });
    
            Button  btnSet = (Button) findViewById(R.id.btnSetDateTime);
            final   DatePicker dp = (DatePicker) findViewById(R.id.datePicker);
            final   TimePicker tp = (TimePicker) findViewById(R.id.timePicker);
    
            btnSet.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String strDateTime = dp.getYear() + "-" + (dp.getMonth() + 1) + "-" + dp.getDayOfMonth() + " "+ tp.getCurrentHour() + ":" + tp.getCurrentMinute();
    
                    Toast.makeText(DateTimeActivity.this, "User has selected " + strDateTime, Toast.LENGTH_LONG).show();
    
                    finish();// move to previous activity
                }});
    
        }       
    
    }
    

    【讨论】:

    • 感谢人的帮助,但它就像在弹出窗口中一样工作。我希望这应该像主屏幕。如果你想要我可以给你我想要的截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多