gpt4 book ai didi

android - 创建包含 TimePicker 和 DatePicker 的对话框时出现问题?

转载 作者:行者123 更新时间:2023-11-29 15:30:47 27 4
gpt4 key购买 nike

我正在创建一个自定义对话框,我想在其中同时使用 DatePicker 和 TimePicker 。虽然我可以根据需要使用 TimePicker 但对于 DatePicker 我缺乏想法。请帮我。我的代码如下

我的 XML 布局文件是...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker>
<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker>
<Button android:id="@+id/buttondatetime" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK" />

我创建自定义对话框的 Java 代码是

void dialogDateTime()
{
dialog = new Dialog(this);
dialog.setContentView(R.layout.datetime);
dialog.setCancelable(true);
dialog.setTitle("This is Dialog 1");
dialog.show();

TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1);
time_picker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minutes)
{
// TODO Auto-generated method stub

}
});

DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1);




Button btn_ok = (Button) dialog.findViewById(R.id.buttondatetime);
btn_ok.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String text = "Time "+hours+":"+minute;
tv_date_time.setText(text);
dialog.cancel();
}
});

}

@覆盖 protected 对话框 onCreateDialog(int id) { //TODO 自动生成方法 stub

    switch (id) 
{
case DIALOG_1:
dialogDateTime();
break;
case DIALOG_2:
dialogTwo();
break;
default:
break;
}
return super.onCreateDialog(id);
}

我没有像在 TimePicker 中那样获得 DatePicker 的任何方法,所以我无法使用 DatePicker请帮忙

最佳答案

感谢您没有回复我自己找到了答案..我这样改变了我的方法

void dialogDateTime()
{
dialog = new Dialog(this);
dialog.setContentView(R.layout.datetime);
dialog.setCancelable(true);
dialog.setTitle("This is Dialog 1");
dialog.show();

TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1);
time_picker.setOnTimeChangedListener(new OnTimeChangedListener()
{
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minutes)
{
// TODO Auto-generated method stub
// Toast.makeText(CustomDialog.this,
// "hourOfDay = "+hourOfDay+"minute = "+minute , 1000).show();
hours = hourOfDay;
minute = minutes;
}
});

final DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1);
Button btn = (Button) dialog.findViewById(R.id.buttondatetime);
btn.setOnClickListener(new OnClickListener()
{

public void onClick(View arg0)
{
// TODO Auto-generated method stub

TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText("You selected " + date_picker.getDayOfMonth() + "/" + (date_picker.getMonth() + 1) + "/"
+ date_picker.getYear());
txt.append("Time "+ hours + ":" +minute);

dialog.cancel();
}

}

);

}

感谢链接 Android Date and Time Controls

关于android - 创建包含 TimePicker 和 DatePicker 的对话框时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com