在Eclipse的Andr​​oid的Java - 使用的DatePicker和TimePicker对话框 [英] Using DatePicker and TimePicker dialog boxes - Android Java in Eclipse

查看:227
本文介绍了在Eclipse的Andr​​oid的Java - 使用的DatePicker和TimePicker对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对code没有错误,但是当我启动我在我的主XML页面,上面写着:

得到一个致命错误
  

03-21 22:00:31.849:E / AndroidRuntime(606):java.lang.RuntimeException的:无法实例活动ComponentInfo {countrycabin.ist236 / countrycabin.ist236.Main}:显示java.lang.NullPointerException

我不知道是什么原因造成的问题。正如我所说,我没有错误。我做了一个计划非常相似,这一点,工作细,我只是说了TimePicker这一个。

我试图做的是pretty的简单。我正在计划推出后,当我点击一个按钮,显示一个对话框,显示无论是一个datepicker或TimePicker,当有人选择词语的一个TextView在底部显示一个日期。我无法测试,即使这一点,但是,因为它甚至不会启动。我的模拟器告诉我,它不会响应。

我使用蜂窝为此,作为一本教科书说。

下面是我所有的code。

主要清单XML

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=countrycabin.ist236
安卓版code =1
机器人:VERSIONNAME =1.0>

<用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =11/>

<应用
    机器人:allowBackup =真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名称=countrycabin.ist236.Main
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

< /舱单>
 

主要XML

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  的xmlns:工具=htt​​p://schemas.android.com/tool​​s
  机器人:ID =@ + ID / LinearLayout1
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent
  机器人:方向=横向
  工具:主上下文=>

< ImageView的
    机器人:ID =@ + ID / imgCabin
    机器人:layout_width =282dp
    机器人:layout_height =match_parent
    机器人:contentDescription =@字符串/ strCabin
    机器人:SRC =@可绘制/舱/>

< TableLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的TableRow>



        <的TextView
            机器人:ID =@ + ID / txtTitle
            机器人:layout_width =WRAP_CONTENT
            机器人:后台=#666FFF
            机器人:填充=50dp
            机器人:文本=@字符串/ strTitle
            机器人:TEXTSIZE =40SP
            机器人:字体=衬/>


    < /的TableRow>

    <的TableRow>



        <的TextView
            机器人:ID =@ + ID / txtDesc
            机器人:layout_width =WRAP_CONTENT
            机器人:填充=25dp
            机器人:文本=@字符串/ strDesc
            机器人:TEXTSIZE =30sp/>

        < /的TableRow>



        < RadioGroup中
            机器人:ID =@ + ID /收音机
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0.20
            机器人:contentDescription =@字符串/ strRad>

            <单选按钮
                机器人:ID =@ + ID / radCabin1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginBottom =5DP
                机器人:layout_marginLeft =250dp
                机器人:检查=真
                机器人:文本=@字符串/ strCabinOne
                机器人:TEXTSIZE =25sp/>

            <单选按钮
                机器人:ID =@ + ID / radCabin2
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_marginLeft =250dp
                机器人:文本=@字符串/ strCabinTwo
                机器人:TEXTSIZE =25sp/>

        < / RadioGroup中>



    <的TableRow>

        <按钮
            机器人:ID =@ + ID / btnDate
            机器人:layout_width =50dp

            机器人:填充=20SP
            机器人:文本=@字符串/ strDate
            机器人:TEXTSIZE =25sp/>



    < /的TableRow>

    <的TableRow>

          <按钮
             机器人:ID =@ + ID / btnTime
             机器人:layout_width =50dp

             机器人:填充=20SP
             机器人:文本=@字符串/ strTime
             机器人:TEXTSIZE =25sp/>


    < /的TableRow>

       <的TableRow>

        <的TextView
            机器人:ID =@ + ID / txtDate
            机器人:layout_gravity =中心
            机器人:填充=20dp
            机器人:TEXTSIZE =25sp/>

    < /的TableRow>

       <的TableRow>

            <的TextView
                机器人:ID =@ + ID / txtTime
                机器人:layout_gravity =中心
                机器人:填充=20dp
                机器人:TEXTSIZE =25sp/>

       < /的TableRow>


< / TableLayout>

< / LinearLayout中>
 

主要类(Java的code)

 包countrycabin.ist236;

进口的java.util.Calendar;

进口android.os.Bundle;
进口android.app.Activity;
进口android.app.DatePickerDialog;
进口android.app.Dialog;
进口android.app.TimePickerDialog;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;
进口android.widget.DatePicker;
进口android.widget.RadioButton;
进口android.widget.TextView;
进口android.widget.TimePicker;

公共类主要扩展活动{

私人诠释currentYear;
私人诠释currentMonth;
私人诠释CURRENTDAY;
静态最终诠释DATE_DIALOG_ID = 0;
静态最终诠释TIME_DIALOG_ID = 1;
诠释小时;
INT分钟;
字符串cabin1 =的木制城堡;
字符串cabin2 =温馨的小点;
私人按钮btDate;
私人按钮btTime;
私人TextView的timeDisplay;
私人TextView的dateDisplay;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    timeDisplay =(TextView中)findViewById(R.id.txtTime);

    dateDisplay =(TextView中)findViewById(R.id.txtDate);

    btDate =(按钮)findViewById(R.id.btnDate);

    btTime =(按钮)findViewById(R.id.btnTime);

    btDate.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            的ShowDialog(DATE_DIALOG_ID);

        }
    });
    最后的日历C = Calendar.getInstance();
    currentYear = c.get(Calendar.YEAR);
    currentMonth = c.get(Calendar.MONTH);
    CURRENTDAY = c.get(Calendar.DAY_OF_MONTH);



     btTime.setOnClickListener(新View.OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        的ShowDialog(TIME_DIALOG_ID);
    }
});
最后的日历D = Calendar.getInstance();
小时= d.get(Calendar.HOUR_OF_DAY);
分钟= d.get(Calendar.MINUTE);



}


受保护的对话框onCreateDialog(INT ID){
    开关(ID){
    案例DATE_DIALOG_ID:
        返回新DatePickerDialog(这一点,reservationDate,currentYear,currentMonth,CURRENTDAY);
    案例TIME_DIALOG_ID:
        返回新TimePickerDialog(这一点,时间日期,小时,分钟,FALSE);
    }
    返回null;
}

私人DatePickerDialog.OnDateSetListener reservationDate =新DatePickerDialog.OnDateSetListener(){
    最后单选C1 =(单选)findViewById(R.id.radCabin1);
    最后单选C2 =(单选)findViewById(R.id.radCabin2);
        @覆盖
    公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
    如果(c1.isChecked()){


        dateDisplay.setText(您的租赁时间设定为+(月+ 1)+ - +日+ - +年+至+(月+ 1)+ - +(日+ 3) + - +,在+ cabin1 +年+);。
    }
    如果(c2.isChecked()){
        dateDisplay.setText(您的租赁时间设定为+(月+ 1)+ - +日+ - +年+至+(月+ 1)+ - +(日+ 3) + - +,在+ cabin2 +年+);。
    }
    }

};

私人TimePickerDialog.OnTimeSetListener TIMEDATE =新TimePickerDialog.OnTimeSetListener(){

    @覆盖
    公共无效onTimeSet(TimePicker观点,诠释小时,INT分钟){
        timeDisplay.setText(您的到达时间将是在+小时+:+分钟+。);
    }
};



@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}

 }
 

解决方案

勾选此code:它做工精细现在

我在你的活动目前取得的XML文件中的一些变化,以及其工作的罚款

但你必须做出一些改变取决于您的要求。

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / LinearLayout1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=横向>

    < ImageView的
        机器人:ID =@ + ID / imgCabin
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:contentDescription =strCabin
        机器人:SRC =@可绘制/ ic_launcher/>

    < TableLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT>

        <的TableRow>

            <的TextView
                机器人:ID =@ + ID / txtTitle
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:后台=#666FFF
                机器人:文本=strTitle
                机器人:TEXTSIZE =40SP
                机器人:字体=衬/>
        < /的TableRow>

        <的TableRow>

            <的TextView
                机器人:ID =@ + ID / txtDesc
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=strDesc
                />
        < /的TableRow>

        <的TableRow>

            < RadioGroup中
                机器人:ID =@ + ID /收音机
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:contentDescription =strRad>

                <单选按钮
                    机器人:ID =@ + ID / radCabin1
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginBottom =5DP
                    机器人:layout_marginLeft =250dp
                    机器人:检查=真
                    机器人:文本=strCabinOne
                    机器人:TEXTSIZE =25sp/>

                <单选按钮
                    机器人:ID =@ + ID / radCabin2
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_marginLeft =250dp
                    机器人:文本=strCabinTwo
                    机器人:TEXTSIZE =25sp/>
            < / RadioGroup中>
        < /的TableRow>

        <的TableRow>

            <按钮
                机器人:ID =@ + ID / btnDate
                机器人:layout_width =50dp
                机器人:填充=20SP
                机器人:文本=strDate
                机器人:TEXTSIZE =25sp/>
        < /的TableRow>

        <的TableRow>

            <按钮
                机器人:ID =@ + ID / btnTime
                机器人:layout_width =50dp
                机器人:填充=20SP
                机器人:文本=strTime
                机器人:TEXTSIZE =25sp/>
        < /的TableRow>

        <的TableRow>

            <的TextView
                机器人:ID =@ + ID / txtDate
                机器人:layout_gravity =中心
                机器人:填充=20dp
                机器人:TEXTSIZE =25sp/>
        < /的TableRow>

        <的TableRow>

            <的TextView
                机器人:ID =@ + ID / txtTime
                机器人:layout_gravity =中心
                机器人:填充=20dp
                机器人:TEXTSIZE =25sp/>
        < /的TableRow>
    < / TableLayout>

< / LinearLayout中>
 

活动:

 公共类主要扩展活动{

    私人诠释currentYear;
    私人诠释currentMonth;
    私人诠释CURRENTDAY;
    静态最终诠释DATE_DIALOG_ID = 0;
    静态最终诠释TIME_DIALOG_ID = 1;
    诠释小时;
    INT分钟;
    字符串cabin1 =的木制城堡;
    字符串cabin2 =温馨的小点;
    私人按钮btDate;
    私人按钮btTime;
    私人TextView的timeDisplay;
    私人TextView的dateDisplay;
    私人单选C1;
    私人单选C2;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        最后的日历C = Calendar.getInstance();
        currentYear = c.get(Calendar.YEAR);
        currentMonth = c.get(Calendar.MONTH);
        CURRENTDAY = c.get(Calendar.DAY_OF_MONTH);
        小时= c.get(Calendar.HOUR_OF_DAY);
        分钟= c.get(Calendar.MINUTE);

        timeDisplay =(TextView中)findViewById(R.id.txtTime);
        dateDisplay =(TextView中)findViewById(R.id.txtDate);
        btDate =(按钮)findViewById(R.id.btnDate);
        btTime =(按钮)findViewById(R.id.btnTime);
        C1 =(单选)findViewById(R.id.radCabin1);
        C2 =(单选)findViewById(R.id.radCabin2);
        btDate.setOnClickListener(新OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                的ShowDialog(DATE_DIALOG_ID);
            }
        });
        btTime.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                的ShowDialog(TIME_DIALOG_ID);
            }
        });
    }

    受保护的对话框onCreateDialog(INT ID){
        开关(ID){
        案例DATE_DIALOG_ID:
            返回新DatePickerDialog(这一点,reservationDate,currentYear,
                    currentMonth,CURRENTDAY);
        案例TIME_DIALOG_ID:
            返回新TimePickerDialog(这一点,时间日期,小时,分钟,FALSE);
        }
        返回null;
    }

    私人DatePickerDialog.OnDateSetListener reservationDate =新DatePickerDialog.OnDateSetListener(){

        公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
            如果(c1.isChecked()){

                dateDisplay.setText(您的租赁时间设置为
                        +(月+ 1)+ - +日+ - +年+来
                        +(月+ 1)+ - +(日+ 3)+ - +年+,在
                        + cabin1 +)。;
            }
            如果(c2.isChecked()){
                dateDisplay.setText(您的租赁时间设置为
                        +(月+ 1)+ - +日+ - +年+来
                        +(月+ 1)+ - +(日+ 3)+ - +年+,在
                        + cabin2 +)。;
            }
        }

    };

    私人TimePickerDialog.OnTimeSetListener TIMEDATE =新TimePickerDialog.OnTimeSetListener(){

        公共无效onTimeSet(TimePicker观点,诠释小时,INT分钟){
            timeDisplay.setText(你的到达时间将在+时间+:
                    +分钟+)。;
        }
    };

    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }

}
 

I have no errors on the code, but when I launch I get a fatal error in my main XML page that says,

03-21 22:00:31.849: E/AndroidRuntime(606): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{countrycabin.ist236/countrycabin.ist236.Main}: java.lang.NullPointerException

I'm not sure what is causing the problem. As I said, I have no errors. I made a program very similar to this that worked fine, I just added the TimePicker to this one.

What I'm trying to do is pretty straightforward. I'm making the program launch, and when I click a button, a dialog displays that shows either a DatePicker or TimePicker, and when someone selects a date words display in a TextView at the bottom. I'm unable to test even this, however, as it will not even launch. My emulator tells me it will not respond.

I'm using Honeycomb for this, as stated in a textbook.

Here is all my code.

Main Manifest XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="countrycabin.ist236"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="11" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="countrycabin.ist236.Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

main XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  tools:context=".Main" >

<ImageView
    android:id="@+id/imgCabin"
    android:layout_width="282dp"
    android:layout_height="match_parent"
    android:contentDescription="@string/strCabin"
    android:src="@drawable/cabin" />

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow>



        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:background="#666FFF"
            android:padding="50dp"
            android:text="@string/strTitle"
            android:textSize="40sp"
            android:typeface="serif" />


    </TableRow>

    <TableRow>



        <TextView
            android:id="@+id/txtDesc"
            android:layout_width="wrap_content"
            android:padding="25dp"
            android:text="@string/strDesc"
            android:textSize="30sp" />

        </TableRow>



        <RadioGroup
            android:id="@+id/radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"
            android:contentDescription="@string/strRad" >

            <RadioButton
                android:id="@+id/radCabin1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="250dp"
                android:checked="true"
                android:text="@string/strCabinOne"
                android:textSize="25sp" />

            <RadioButton
                android:id="@+id/radCabin2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="250dp"
                android:text="@string/strCabinTwo"
                android:textSize="25sp" />

        </RadioGroup>



    <TableRow>

        <Button
            android:id="@+id/btnDate"
            android:layout_width="50dp"

            android:padding="20sp"
            android:text="@string/strDate"
            android:textSize="25sp" />



    </TableRow>

    <TableRow>

          <Button
             android:id="@+id/btnTime"
             android:layout_width="50dp"

             android:padding="20sp"
             android:text="@string/strTime"
             android:textSize="25sp" />


    </TableRow>

       <TableRow>

        <TextView
            android:id="@+id/txtDate"
            android:layout_gravity="center"
            android:padding="20dp"
            android:textSize="25sp" />

    </TableRow>

       <TableRow>

            <TextView
                android:id="@+id/txtTime"
                android:layout_gravity="center"
                android:padding="20dp"
                android:textSize="25sp" />

       </TableRow>


</TableLayout>

</LinearLayout>

Main class (Java code)

package countrycabin.ist236;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker; 
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class Main extends Activity {

private int currentYear;
private int currentMonth;
private int currentDay;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
int hour;
int minute;
String cabin1 = "The Wooden Castle";
String cabin2 = "Cozy Little Spot";
private Button btDate;
private Button btTime;
private TextView timeDisplay;
private TextView dateDisplay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    timeDisplay = (TextView) findViewById(R.id.txtTime);

    dateDisplay = (TextView) findViewById(R.id.txtDate);

    btDate = (Button) findViewById(R.id.btnDate);

    btTime = (Button) findViewById(R.id.btnTime);

    btDate.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);

        }
    });
    final Calendar c = Calendar.getInstance();
    currentYear = c.get(Calendar.YEAR);
    currentMonth = c.get(Calendar.MONTH);
    currentDay = c.get(Calendar.DAY_OF_MONTH);



     btTime.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v){
        showDialog(TIME_DIALOG_ID);
    }
});
final Calendar d = Calendar.getInstance();
hour = d.get(Calendar.HOUR_OF_DAY);
minute = d.get(Calendar.MINUTE);



}


protected Dialog onCreateDialog(int id) {
    switch(id){
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, reservationDate, currentYear, currentMonth, currentDay);
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, timeDate, hour, minute, false);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener() {
    final RadioButton c1 = (RadioButton) findViewById(R.id.radCabin1);
    final RadioButton c2 = (RadioButton) findViewById(R.id.radCabin2);
        @Override
    public void onDateSet(DatePicker view, int year, int month, int day){
    if(c1.isChecked()){


        dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin1 + ".");
    }
    if(c2.isChecked()){
        dateDisplay.setText("Your rental time is set for " + (month + 1) + "-" + day + "-" + year + " to " + (month + 1) + "-" + (day + 3) + "-" + year + " in " + cabin2 + ".");
    }
    }

};

private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() {

    @Override
    public void onTimeSet(TimePicker view, int hours, int minutes) {
        timeDisplay.setText("Your arrival time will be at " + hours + ":" + minutes + ".");
    }
};



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

解决方案

Check this Code: Its working fine now.

I have made some change in XML file as well as in Your Activity now its working fine

But You have to made some changes depend on your requirement.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgCabin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="strCabin"
        android:src="@drawable/ic_launcher" />

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow>

            <TextView
                android:id="@+id/txtTitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#666FFF"
                android:text="strTitle"
                android:textSize="40sp"
                android:typeface="serif" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/txtDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="strDesc"
                />
        </TableRow>

        <TableRow>

            <RadioGroup
                android:id="@+id/radio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="strRad" >

                <RadioButton
                    android:id="@+id/radCabin1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="250dp"
                    android:checked="true"
                    android:text="strCabinOne"
                    android:textSize="25sp" />

                <RadioButton
                    android:id="@+id/radCabin2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="250dp"
                    android:text="strCabinTwo"
                    android:textSize="25sp" />
            </RadioGroup>
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btnDate"
                android:layout_width="50dp"
                android:padding="20sp"
                android:text="strDate"
                android:textSize="25sp" />
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btnTime"
                android:layout_width="50dp"
                android:padding="20sp"
                android:text="strTime"
                android:textSize="25sp" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/txtDate"
                android:layout_gravity="center"
                android:padding="20dp"
                android:textSize="25sp" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/txtTime"
                android:layout_gravity="center"
                android:padding="20dp"
                android:textSize="25sp" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Activity :

public class Main extends Activity {

    private int currentYear;
    private int currentMonth;
    private int currentDay;
    static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;
    int hour;
    int minute;
    String cabin1 = "The Wooden Castle";
    String cabin2 = "Cozy Little Spot";
    private Button btDate;
    private Button btTime;
    private TextView timeDisplay;
    private TextView dateDisplay;
    private RadioButton c1;
    private RadioButton c2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Calendar c = Calendar.getInstance();
        currentYear = c.get(Calendar.YEAR);
        currentMonth = c.get(Calendar.MONTH);
        currentDay = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        timeDisplay = (TextView) findViewById(R.id.txtTime);
        dateDisplay = (TextView) findViewById(R.id.txtDate);
        btDate = (Button) findViewById(R.id.btnDate);
        btTime = (Button) findViewById(R.id.btnTime);
        c1 = (RadioButton) findViewById(R.id.radCabin1);
        c2 = (RadioButton) findViewById(R.id.radCabin2);
        btDate.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
            }
        });
        btTime.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });
    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, reservationDate, currentYear,
                    currentMonth, currentDay);
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, timeDate, hour, minute, false);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int month, int day) {
            if (c1.isChecked()) {

                dateDisplay.setText("Your rental time is set for "
                        + (month + 1) + "-" + day + "-" + year + " to "
                        + (month + 1) + "-" + (day + 3) + "-" + year + " in "
                        + cabin1 + ".");
            }
            if (c2.isChecked()) {
                dateDisplay.setText("Your rental time is set for "
                        + (month + 1) + "-" + day + "-" + year + " to "
                        + (month + 1) + "-" + (day + 3) + "-" + year + " in "
                        + cabin2 + ".");
            }
        }

    };

    private TimePickerDialog.OnTimeSetListener timeDate = new TimePickerDialog.OnTimeSetListener() {

        public void onTimeSet(TimePicker view, int hours, int minutes) {
            timeDisplay.setText("Your arrival time will be at " + hours + ":"
                    + minutes + ".");
        }
    };

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这篇关于在Eclipse的Andr​​oid的Java - 使用的DatePicker和TimePicker对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆