2014-09-24 37 views
0

今天伊馬試圖建立一個簡單的應用程序,它會提示用戶通過按鍵使用DatePickerDialog插入2日,然後執行一個查詢到數據庫:的Android DatePickerDialog等待解僱

 Button.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 


      Da = new DatePickerDialog(MainActivity.this, DADateSetListener, 2014, 8, 24); 
      Da.show(); 
      Da.setTitle("Date FROM"); 

      A = new DatePickerDialog(MainActivity.this, ADateSetListener, 2014, 10, 24); 

      A.show(); 
      A.setTitle("Date TO"); 


      //Erase the List View 
      List.setAdapter(null); 

      prepareProgressBar(); 
      query(); 

     } 

    }); 

我唯一的問題是: 如何等待DatePickerDialog做解僱或等待用戶輸入輸入,然後執行查詢?

回答

0

the docs

我們建議您使用DialogFragment每次或日期選擇器來承載。

該文檔包括延伸DialogFragment(位於here)樣品等級:

public static class TimePickerFragment extends DialogFragment 
          implements TimePickerDialog.OnTimeSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current time as the default values for the picker 
     final Calendar c = Calendar.getInstance(); 
     int hour = c.get(Calendar.HOUR_OF_DAY); 
     int minute = c.get(Calendar.MINUTE); 

     // Create a new instance of TimePickerDialog and return it 
     return new TimePickerDialog(getActivity(), this, hour, minute, 
       DateFormat.is24HourFormat(getActivity())); 
    } 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // Do something with the time chosen by the user 
    } 
} 

你會顯示它像這樣(再次,從the docs代碼):

public void showTimePickerDialog(View v) { 
    DialogFragment newFragment = new TimePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "timePicker"); 
} 

而且,作爲額外的獎勵,這個對話確實是modal(當你說「wa它用於解僱「)。

+0

謝謝,我會試試看。 – Vesco 2014-09-25 08:51:02

0

這是一個簡單的對話框我不明白爲什麼你不使用默認的監聽器:

DatePickerDialog dpd; 
dpd.setOnDismissListener(new DialogInterface.OnDismissListener() { 
    @Override 
    public void onDismiss(DialogInterface dialog) { 
     // Handle dismiss 
    } 
}); 
dpd.setOnKeyListener(new DialogInterface.OnKeyListener() { 
    @Override 
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 
     // Handle clicks 
     return false; 
    } 
}); 
+0

謝謝,我會試一試。 – Vesco 2014-09-25 08:50:31