开发者

problems with date picker in an android app

hi i my app i have placed two edit text boxes, when i touch on it the date picker dialog box gets appeared.

Now the problem is when i touch the first edit box the dialog opens and after setting it displays at the EditText1. Then when i touch the second edit box the dialog opens and after setting some other date, it is not displayed in EditText2, instead 开发者_开发技巧it is show in the EditText1 and the former date gets changed

I want the dates to be displayed in respective boxes.

The following is my code

{
    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;
    }
}

the following are some more problems i am facing here,

  1. Whenever i touch the edit text box the keyboard also gets opened how to hide the keyboard

  2. in the EditText box i want the order to be viewed as Year/Month/Date

  3. Is there any way to change the date picker dialog box as the below figure

problems with date picker in an android app

please help me in this


Check this code..

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();
    }
};

}


You're setting the same id for both show dialog methods...

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);
    }
});

...and your switch/case block only has one 'case'....

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

As for the other issues, split them into separate questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜