2013-04-28 54 views
0

我的頁面上有兩個按鈕。當按下按鈕時,打開選擇器對話框。點擊日期選擇器的完成後,我想將我的按鈕文本設置爲用戶選擇的日期。Android:從DatePicker獲取返回值/日期集

我想使用datepicker的單個函數來做到這一點。

我的代碼:

final Button fromdate = (Button) dialog.findViewById(R.id.ButtonTripConfigureFrom);  
    fromdate.setOnClickListener(new OnClickListener() 
    {     
     public void onClick(View v) 
      {       
       showDialog(0);  
       fromdate.setText(date); 
      } 
    }); 

final Button tilldate = (Button) dialog.findViewById(R.id.ButtonTripConfigureTo);  
    tilldate.setOnClickListener(new OnClickListener() 
    {     
     public void onClick(View v) 
      {       
       showDialog(0); 
       tilldate.setText(date); 
      } 
    }); 

protected Dialog onCreateDialog(int id) 
    { 
     final Calendar c = Calendar.getInstance(); 
     int myYear = c.get(Calendar.YEAR); 
     int myMonth = c.get(Calendar.MONTH); 
     int myDay = c.get(Calendar.DAY_OF_MONTH); 

    return new DatePickerDialog(this,myDateSetListener,myYear, myMonth, myDay); 
} 

private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() 
{ 
    public void onDateSet(DatePicker view, int year, int month, int day) 
    {   
     date = day + "/" + (month+1) +"/"+ year;      
    } 
}; 

然而,當我做到這一點,用戶選擇的值不會顯示。當我再次按下按鈕時,它纔會顯示。

我已經檢查了所有的解決方案(按鈕的setText正在做的日期選擇器用戶按下之前執行)表示dateset函數內部修改。但是因爲我有兩個不同的按鈕,所以我不能使用dateset函數內的按鈕的settext。

是否有通過該值是按日期設置功能或通過其參數可以傳遞給dateset的方式返回,然後用,如果那裏面dateset任何方式。

回答

1

只有當您點擊它時,您才從更新按鈕進行更新。這就是爲什麼當它再次點擊時更新它... 要設置日期時更新它,你必須改變它在myDateSetListener.onDateSet中的文本,但是當你有兩個按鈕時,在你的buttons.onClick中設置一些值要記住哪個按鈕被點擊:



    mFromdate = (Button) dialog.findViewById(R.id.ButtonTripConfigureFrom);  
    mFromdate.setOnClickListener(new OnClickListener() 
    {     
     public void onClick(View v) 
      {       
       showDialog(0); 
       mButtonClicked = 0; 
      } 
    }); 

    mTilldate = (Button) dialog.findViewById(R.id.ButtonTripConfigureTo);  
    mTilldate.setOnClickListener(new OnClickListener() 
    {     
     public void onClick(View v) 
      {       
       showDialog(0); 
       mButtonClicked = 1; 
      } 
    }); 

    private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() 
    { 

     public void onDateSet(DatePicker view, int year, int month, int day) 
     {   
      if(mButtonClicked == 0) 
       mFromdate.setText(day + "/" + (month+1) +"/"+ year); 
      else if(mButtonClicked == 1) 
       mTillDate.setText(day + "/" + (month+1) +"/"+ year);   
     } 

    }; 

同時,讓你的類mFromdate,mTilldate和mButtonclicked領域;)

+1

感謝您的解決方案。 – 2013-04-29 14:30:43

+0

非常歡迎:) – 2013-05-07 20:27:44

0

所有我能想到的現在是: -

static int buttonClicked;  
final Button fromdate = (Button) dialog.findViewById(R.id.ButtonTripConfigureFrom);  
     fromdate.setOnClickListener(new OnClickListener() 
     {     
      public void onClick(View v) 
       {  
        buttonClicked = fromDate.getId(); 
        showDialog(0); 
       } 
     }); 

final Button tilldate = (Button) dialog.findViewById(R.id.ButtonTripConfigureTo);  
    tilldate.setOnClickListener(new OnClickListener() 
    {     
     public void onClick(View v) 
      {   
       buttonClicked = tillDate.getId();     
       showDialog(0); 
      } 
    }); 

protected Dialog onCreateDialog(int id) 
    { 
     final Calendar c = Calendar.getInstance(); 
     int myYear = c.get(Calendar.YEAR); 
     int myMonth = c.get(Calendar.MONTH); 
     int myDay = c.get(Calendar.DAY_OF_MONTH); 

    return new DatePickerDialog(this,myDateSetListener,myYear, myMonth, myDay); 
} 

private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener() 
{ 
    public void onDateSet(DatePicker view, int year, int month, int day) 
    {   
     date = day + "/" + (month+1) +"/"+ year; 
     Button btn = (Button) dialog. findViewById(buttonClicked); //assuming the dialog in which the button is a class level variable.. 
     btn.setText(date);     
    } 
}; 

這應該肯定做到這一點。 :)

編輯 - 我做你的代碼是什麼我保存被按下的按鈕的ID。然後在onDateSet()方法中從此存儲的標識中檢索按鈕,並將所選日期設置爲該按鈕的文本。

+0

感謝您的解決方案。你的解決方案非常好,因爲它刪除了if子句,但它不適用於我,因爲這兩個按鈕位於對話框內。 – 2013-04-29 14:32:34

+0

對話框裏面指的是哪個對話框?你可以發佈截圖或其他? – d3m0li5h3r 2013-04-29 15:17:01

+0

好的,我明白了..在onDateSet()方法中編輯語句.. – d3m0li5h3r 2013-04-29 15:18:20