2011-08-09 73 views
1

我正在創建一個自定義對話框,我要使用DatePicker和TimePicker。儘管我可以使用TimePicker,但是對於DatePicker,我缺乏創意。 請幫幫我。 我的代碼如下創建包含TimePicker和DatePicker的對話框的問題?

我的XML佈局文件...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TimePicker android:id="@+id/timePicker1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></TimePicker> 
<DatePicker android:id="@+id/datePicker1" 
    android:layout_width="wrap_content" android:layout_height="wrap_content"></DatePicker> 
<Button android:id="@+id/buttondatetime" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:text="OK" /> 

和我的Java代碼創建自定義對話框是

void dialogDateTime() 
{ 
    dialog = new Dialog(this); 
    dialog.setContentView(R.layout.datetime); 
    dialog.setCancelable(true); 
    dialog.setTitle("This is Dialog 1"); 
    dialog.show(); 

    TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1); 
    time_picker.setOnTimeChangedListener(new OnTimeChangedListener() 
    { 
     @Override 
     public void onTimeChanged(TimePicker view, int hourOfDay, int minutes) 
     { 
      // TODO Auto-generated method stub 

     } 
    }); 

    DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1); 




    Button btn_ok = (Button) dialog.findViewById(R.id.buttondatetime); 
    btn_ok.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      String text = "Time "+hours+":"+minute; 
      tv_date_time.setText(text); 
      dialog.cancel(); 
     } 
    }); 

} 

@覆蓋 保護對話框onCreateDialog(int id) { // TODO自動生成方法存根

switch (id) 
    { 
    case DIALOG_1: 
     dialogDateTime(); 
     break; 
    case DIALOG_2: 
     dialogTwo(); 
     break; 
    default: 
     break; 
    } 
    return super.onCreateDialog(id); 
} 

我沒有得到任何方法的DatePicker在TimePicker所以我不能夠使用的DatePicker 請幫助

+0

的問題是如何使用XML佈局創建的DatePicker查看。 – AB1209

回答

5

感謝不回答我找到答案我自己.. 我改變了我的方法這樣

void dialogDateTime() 
{ 
    dialog = new Dialog(this); 
    dialog.setContentView(R.layout.datetime); 
    dialog.setCancelable(true); 
    dialog.setTitle("This is Dialog 1"); 
    dialog.show(); 

    TimePicker time_picker = (TimePicker) dialog.findViewById(R.id.timePicker1); 
    time_picker.setOnTimeChangedListener(new OnTimeChangedListener() 
    { 
     @Override 
     public void onTimeChanged(TimePicker view, int hourOfDay, int minutes) 
     { 
      // TODO Auto-generated method stub 
      // Toast.makeText(CustomDialog.this, 
      // "hourOfDay = "+hourOfDay+"minute = "+minute , 1000).show(); 
      hours = hourOfDay; 
      minute = minutes; 
     } 
    }); 

    final DatePicker date_picker = (DatePicker) dialog.findViewById(R.id.datePicker1); 
    Button btn = (Button) dialog.findViewById(R.id.buttondatetime); 
    btn.setOnClickListener(new OnClickListener() 
    { 

     public void onClick(View arg0) 
     { 
      // TODO Auto-generated method stub 

      TextView txt = (TextView) findViewById(R.id.textView1); 
      txt.setText("You selected " + date_picker.getDayOfMonth() + "/" + (date_picker.getMonth() + 1) + "/" 
        + date_picker.getYear()); 
      txt.append("Time "+ hours + ":" +minute); 

      dialog.cancel(); 
     } 

    } 

    ); 

} 

感謝鏈接Android Date and Time Controls

+0

+1不錯,你找到了解決方案,並在這裏發佈它作爲參考。 – PravinCG

相關問題