简体   繁体   English

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

[英]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. 现在的问题是,当我触摸第一个编辑框时,对话框将打开,并在设置后将其显示在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 然后,当我触摸第二个编辑框时,对话框将打开,并在设置了其他日期后,它不会显示在EditText2中,而是显示在EditText1中,并且以前的日期会更改

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 在EditText框中,我希望订单显示为年/月/日

  3. Is there any way to change the date picker dialog box as the below figure 有什么方法可以更改日期选择器对话框,如下图

在此处输入图片说明

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... 您正在为两个显示对话框方法设置相同的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);
    }
});

...and your switch/case block only has one 'case'.... ...并且您的开关/外壳块只有一个'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. 至于其他问题,请将其分为单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM