我在下面的相同 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 获取时间?