多DatePickers动态地在同一个活动的android [英] Multiple DatePickers dynamically in same activity android

查看:91
本文介绍了多DatePickers动态地在同一个活动的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了很多文章创建日期选择器,但在我来说,我必须动态地创建多个datepickers和更新多个相应EditTexts的日期。但在我的案件之日起只在最后的EditText更新。的EditText和日期选择器的数量可能会有所不同,但在这里,我以两个。我已附后快照。这里是我的示例code。

I have read many articles for creating datepicker but in my case I have to create multiple datepickers dynamically and update the date in multiple corresponding EditTexts. But in my case the date is updated only at the last EditText. The number of EditText and datepicker may vary but here I am taking two. I have attached the snapshot herewith. And here is my sample code.

 public datepicker(){
   EditText txtBox = new EditText(context);
   Button btn = new Button(context);
      btn.setOnClickListener(new Button.OnClickListener() {
     public void onClick(View v) {              
        DatePickerDialog dialog= new DatePickerDialog(act, mDateSetListener,
                       mYear, mMonth, mDay); 
            dialog.show();                              
        }
    });     

 }



    private DatePickerDialog.OnDateSetListener mDateSetListener = new   
    DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }

  private void updateDisplay() {    
    txtBox.setText(new StringBuilder().append(mMonth + 1).append("-")
     .append(mDay).append("").append(mYear).append(""));
    }

什么是错误的,我code。 先谢谢了。

What's wrong in my code. Thanks in advance.

推荐答案

我有同样的问题,我用下面的code。只要使用ID为每个日期选择器对话框,并执行每个ID一些行动。

I had same problem and I used following code. Just use id for each datepicker dialog and perform some action for each id.

 private final int BIRTH_DATE_DIALOG=4000;
 private final int ANNIVERSARY_DATE_DIALOG=5000;


@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub

    Calendar cal = Calendar.getInstance();
    DatePickerDialog datePicker = new DatePickerDialog(YourActivity.this,new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
                            EditText edt_Bdate=(EditText)findViewById(id);
                            edt_Bdate.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth);


        }
    },cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH));
    return datePicker;
}

edittext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // pass id here
            showDialog(edittext.getId());
        }
    });

这篇关于多DatePickers动态地在同一个活动的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆