Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用xml时间选择器?_Android - Fatal编程技术网

Android 如何使用xml时间选择器?

Android 如何使用xml时间选择器?,android,Android,我已经用下面相同的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:orientat

我已经用下面相同的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代码创建了dialog,并从xml代码中引入了dialog和timepicker,如下所示:

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();
}
public void TimePickerDialog(){
最终对话框=新对话框(本);
setContentView(R.layout.sign_in_对话框);
/////////////////////更新代码////////////////////
TimePicker timePicker1=(TimePicker)findViewById(R.id.timePicker1);
timePicker1.setIs24HourView(true);
/////////////////////////////////
dialog.setTitle(“快速静音”);
//允许取消该对话框
对话框。可设置可取消(true);
//在这里,我们将功能添加到对话框的内容中
//参照按钮
按钮ok按钮=(按钮)对话框.findViewById(R.id.dialog\u ok);
//创建一个单击侦听器
setOnClickListener(新的OnClickListener(){
//重写onClick函数以执行某些操作
公共void onClick(视图v){
}
});
//参照按钮
Button cancelButton=(Button)dialog.findViewById(R.id.dialog\u cancel);
//创建一个单击侦听器
cancelButton.setOnClickListener(新的OnClickListener(){
//重写onClick函数以执行某些操作
公共void onClick(视图v){
//关闭对话框
dialog.dismise();
}
});
//最后显示对话框
dialog.show();
}

我遇到的问题是,如何访问用xml代码创建的计时器,以便使计时器成为24小时计时器。。。还有,如何从xml代码中的时间选择器中获取时间?

就像通常所做的那样:

TimePicker timePicker = (TimePicker)dialog.findViewById(R.id.timePicker1);
和往常一样,这只在调用了
对话框后才起作用。setContentView
。然后,要选择时间,请拨打:

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

不能在XML中设置24小时模式,请改用
MyTimePicker.setIs24HourView(布尔值)


请参见有关如何获取时间的示例

以下是获取时间选择器实例的步骤:

  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);

当我尝试获取timePicker.getCurrentHour()时;timePicker.getCurrentMinute();它给了我nullpointeexception?@john:你做错了什么,找不到
时间选择器
视图。编辑您的问题以包含更新的代码并让我知道。@john:我错了,找到了
时间选择器
,但是
getCurrentHour
getCurrentMinute
没有初始值。您应该检查他们的结果是否为
null
。此外,您可能希望在按下“确定”时访问这些值,因此您需要设置
计时器
最终
。再次抱歉,还有一个问题。我使用timePicker1.setIs24HourView(布尔值);让它持续到24小时,但我得到了nullpointerexception?@john:你确定
sign\u in\u dialog
是你展示给我们的布局吗?