4

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

4

3 回答 3

3

就像它通常做的那样:

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

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

timePicker.getCurrentHour();
timePicker.getCurrentMinute();
于 2012-06-10T20:00:22.780 回答
1

您不能在 XML 中设置 24 小时模式,请MyTimePicker.setIs24HourView(boolean)改用。

请参阅示例以了解如何获取时间。

于 2012-06-10T20:02:42.787 回答
0

以下是获取 TimePicker 实例的步骤:

  final Dialog mTimeDialog = new Dialog(this);
  final RelativeLayout mTimeDialogView = (RelativeLayout)getLayoutInflater().inflate(R.layout.time_dialog, null);

  final TimePicker mTimePicker = (TimePicker) mTimeDialogView.findViewById(R.id.TimePicker);

  mTimePicker.setIs24HourView(true);
于 2012-06-11T12:47:22.870 回答