Java Android:如何从日期和时间选择器获取值
我正在构建一个应用程序,试图让用户选择时间,因此我使用了Java Android:如何从日期和时间选择器获取值,java,android,date,time,datepicker,Java,Android,Date,Time,Datepicker,我正在构建一个应用程序,试图让用户选择时间,因此我使用了日期选择器和时间选择器以及按钮集。所以,当他点击设置按钮时,一个对话框就会出现,就像你选择了x日期和x时间一样,或者以任何方式在屏幕上显示一条信息,就像用户这次选择的一样。因此,我构建了代码,但当我的应用程序进入该活动时,它总是被强制停止。与显示数据相关的xml文件没有问题,因为当我对从datePicker和timePicker获取值的java行进行注释时,应用程序工作绝对正常。 我仍然张贴这两个文件的代码,以便于理解。还将发布日志cat异
日期选择器
和时间选择器
以及按钮集
。所以,当他点击设置按钮时,一个对话框就会出现,就像你选择了x日期和x时间一样,或者以任何方式在屏幕上显示一条信息,就像用户这次选择的一样。因此,我构建了代码,但当我的应用程序进入该活动时,它总是被强制停止。与显示数据相关的xml文件
没有问题,因为当我对从datePicker
和timePicker
获取值的java行进行注释时,应用程序工作绝对正常。
我仍然张贴这两个文件的代码,以便于理解。还将发布日志cat异常。我已经删除了所有不必要的代码,如导入和包以及额外的按钮等,以使其快速可读
这是.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>
这是logcat
异常:
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();
}
}
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
您的问题似乎在以下方面:
ScrollView DTPicker = (ScrollView) View.inflate(TimeDate.this,R.layout.time_date, null);
您正在将包含LinearLayout
的XML扩展为根节点,但是您正在将其类型转换为ScrollView
导致ClassCastException
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);
请尝试以下代码
time\u 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>
不要做任何额外的事情,你的代码只是完美的。只要删除
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
}});
如何获取这些值也有同样的问题。。请有人回答我将粗暴地投他/她一票:)对不起,伙计,但仍然不工作。这是日志cat异常W/dalvikvm(1007):threadid=1:线程以未捕获异常(group=0x4001d800)E/AndroidRuntime(1007):致命异常:main E/AndroidRuntime(1007):java.lang.RuntimeException:无法启动活动组件信息{com.ogtpl.android/com.ogtpl.android.TimeDate}:java.lang.IllegalStateException:指定的子级已具有父级。你必须先给孩子的父母打电话removeView()。嘿,请试着帮个忙,我会严厉地接受并同意你的回答。好吧,我的错误,对不起,我以前遇到过几个书呆子,他们来这里只是为了名誉。很抱歉,伙计,但如果你能解决这个问题,我真的会感谢你。创建一个scrollview的xml文件,通过充气器充气,然后将其设置为警报对话框,这样肯定能正常工作。我知道,因为我已经这样做了,但我想在一个xml屏幕上完成,所以有没有办法在一个屏幕上完成呢谢谢你的帮助,但它就像一个弹出窗口一样工作。我希望这应该像主屏幕一样。如果你想要的话,我可以给你一个我想要的截图。现在对6.0+使用getHour()。
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
}});