Android应用中日期选择器的问题

时间:2011-03-26 07:40:53

标签: java android datepicker

您好我的应用程序我已经放置了两个编辑文本框,当我触摸它时会出现日期选择器对话框。

现在的问题是,当我触摸第一个编辑框时,对话框打开,设置完成后会显示在EditText1上。然后,当我触摸第二个编辑框时,对话框打开,在设置其他日期后,它不会显示在EditText2中,而是显示在EditText1中,前一个日期会更改

我希望日期显示在相应的框中。

以下是我的代码

{
    et1 =(EditText)findViewById(R.id.widget29);
    et1.setHint("DOB");
    et2 =(EditText)findViewById(R.id.widget32);
    et2.setHint("DOF");

    et1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            showDialog(DATE_DIALOG_ID);
        }
    });

    et2.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            showDialog(DATE_DIALOG_ID);
        }
    });


 // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

}


private void updateDisplay()
{
    this.et1.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append("-"));
}


private void updateDisplay1()
{
    this.et2.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append("-"));
}

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 DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener()
    {
        public void onDateSet(DatePicker view, int year,
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDisplay1();
        }
    };

    @Override
    protected Dialog onCreateDialog(int id) 
    {
       switch (id) 
       {
          case DATE_DIALOG_ID:
          return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
       }
       return null;
    }
}

以下是我在这里遇到的一些问题,

  1. 每当我触摸编辑文本框时,键盘也会打开,如何隐藏键盘

  2. 在EditText框中的
  3. 我希望订单被视为年/月/日

  4. 有没有办法更改日期选择器对话框,如下图所示

  5. enter image description here

    请帮助我

2 个答案:

答案 0 :(得分:4)

检查此代码..

public class Main extends Activity {
EditText et1,et2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_ID1 = 1;
private int mYear;
private int mMonth;
private int mDay;

private int mYear1;
private int mMonth1;
private int mDay1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    et1=(EditText)findViewById(R.id.EditText01);
    et2=(EditText)findViewById(R.id.EditText02);

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    et1.setText("month/year");
    et2.setText("month/year");
    et1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });

    et2.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID1);
            return false;
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);

    case DATE_DIALOG_ID1:
        return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1,
                mDay1);
    }
    return null;
}

// updates the date in the TextView

private void updateDisplay() {
    et1.setText(new StringBuilder()
    // Month is 0 based so add 1
            .append(mMonth + 1).append("-").append(mYear));
}
private void updateDisplay1() {

    et2.setText(new StringBuilder()
    // Month is 0 based so add 1
            .append(mMonth1 + 1).append("-").append(mYear1));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year1, int monthOfYear1,
            int dayOfMonth1) {
        // TODO Auto-generated method stub
        mYear1 = year1;
        mMonth1 = monthOfYear1;
        mDay1 = dayOfMonth1;
        updateDisplay1();
    }
};

}

答案 1 :(得分:1)

您为两个节目对话框方法设置相同的ID ...

et1.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        showDialog(DATE_DIALOG_ID);
    }
});

et2.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        showDialog(DATE_DIALOG_ID);
    }
});

...而你的开关/案例块只有一个'case'....

   switch (id) 
   {
      case DATE_DIALOG_ID:
      return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
   }

至于其他问题,请将它们分成单独的问题。