【问题标题】:Android : Get return value from DatePicker / Date SetAndroid:从 DatePicker / Date Set 获取返回值
【发布时间】:2013-04-29 23:19:52
【问题描述】:

我的页面上有两个按钮。按下按钮时,日期选择器对话框打开。单击日期选择器的完成后,我想将按钮文本设置为用户选择的日期。

我想使用 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;                    
    } 
};

但是,当我这样做时,不会显示用户选择的值。它仅在我再次按下按钮时显示。(在用户按下日期选择器的 DONE 之前正在执行按钮的 settext)

我检查过的所有解决方案都表明在 dateset 函数中进行了修改。但由于我有两个不同的按钮,我不能在 dateset 函数中使用按钮的 settext。

有什么方法可以通过日期集函数返回值,或者可以将参数传递给日期集,然后在日期集中使用if then。

【问题讨论】:

    标签: android


    【解决方案1】:

    只有当您单击它时,您才会更新 fromdate 按钮。这就是为什么当您再次单击它时它会更新... 要在设置日期时更新它,您必须在 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 字段;)

    【讨论】:

      【解决方案2】:

      目前我能想到的只有:-

      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() 方法中从这个存储的 id 中检索按钮,并将所选日期设置为该按钮的文本。..

      【讨论】:

      • 感谢您的解决方案。您的解决方案非常好,因为它删除了 if 子句,但它对我不起作用,因为两个按钮位于对话框中。
      • 对话框里面的意思是哪个对话框??能发个截图什么的吗??
      猜你喜欢
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多