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

时间:2012-04-27 07:41:29

标签: java android date time datepicker

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

这是timePicker档案

的代码
.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

4 个答案:

答案 0 :(得分:11)

不要做任何额外的事情,你的代码只是完美的。只需删除ScrollLayout的代码或其属性,无论您在哪里使用它。 Lemme从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
            }});

答案 1 :(得分: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);

答案 2 :(得分:0)

你的问题似乎就在这一行:

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

您正在将包含LinearLayout的XML作为根节点进行膨胀,但是您将其输入ScrollView并导致ClassCastException

答案 3 :(得分:0)

尝试以下代码......

<强> 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
            }});

    }       

}
相关问题