2012-06-10 34 views
4

我有對話框按鈕,然後在下面的相同的XML代碼創建timepicker:如何使用xml timepicker?

<?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代碼對話框並timepicker在如下showen:

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代碼創建的,這樣我可以讓timepicker24小時timepicker的timepicker? ...以及我如何從xml代碼中的timepicker獲取時間?

回答

3

就像它通常做:

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

和往常一樣dialog.setContentView後,這隻能叫做。然後,讓挑時間,通話時:

timePicker.getCurrentHour(); 
timePicker.getCurrentMinute(); 
+0

當我試圖讓timePicker.getCurrentHour(); timePicker.getCurrentMinute();它給我nullpointeexception? – john

+0

@約翰:你正在做的事情錯了,找不到'TimePicker'視圖。編輯你的問題,包括更新的代碼,並讓我知道。 –

+0

嘿我更新了代碼 – john

1

您不能設置的24小時內模式的XML,使用MyTimePicker.setIs24HourView(boolean)來代替。

查看如何讓時間this例子。

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