gpt4 book ai didi

android - 如何使用xml时间选择器?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:25 26 4
gpt4 key购买 nike

我在下面的相同 xml 代码中创建了对话框按钮和时间选择器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:id="@+id/dialog_ok" android:text="OK" android:layout_width="fill_parent" android:layout_weight="1"></Button>
<Button android:layout_height="wrap_content" android:text="Cancel" android:id="@+id/dialog_cancel" android:layout_width="fill_parent" android:layout_weight="1"></Button>
</LinearLayout>

</LinearLayout>

然后我在 java 代码中创建了对话框,并从 xml 代码中引入了对话框和时间选择器,如下所示:

public void TimePickerDialog(){
final Dialog dialog = new Dialog(this);

dialog.setContentView(R.layout.sign_in_dialog);

/////////////////////updated code////////////////////
TimePicker timePicker1 = (TimePicker) findViewById(R.id.timePicker1);

timePicker1.setIs24HourView(true);
/////////////////////////////////

dialog.setTitle("Quick Mute");

//Allow the dialog to be cancelable
dialog.setCancelable(true);
// Here we add functionality to our dialog box's content. In this example it's the two buttons
//reference the button
Button okButton = (Button) dialog.findViewById(R.id.dialog_ok);
//Create a click listener
okButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {

}
});

//reference the button
Button cancelButton = (Button) dialog.findViewById(R.id.dialog_cancel);
//Create a click listener
cancelButton.setOnClickListener(new OnClickListener() {
//override the onClick function to do something
public void onClick(View v) {
//Close the dialog</span>
dialog.dismiss();
}
});
//finally show the dialog
dialog.show();
}

我遇到的问题是如何访问在 xml 代码中创建的时间选择器,以便我可以使时间选择器成为 24 小时时间选择器? ...以及如何从 xml 代码中的时间选择器获取时间?

最佳答案

就像通常做的那样:

TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker1);

和往常一样,这仅在调用 dialog.setContentView 后有效。然后,要选择时间,请调用:

timePicker.getCurrentHour();
timePicker.getCurrentMinute();

关于android - 如何使用xml时间选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10971886/

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