質問

at now i want to ask, i have trying to create 2 datetimepicker and trying to get the value in two different editText (textbox) in android, here's my code, i have Sign the comment in my code below.

public class CreateData extends Activity implements OnClickListener{
    int hour, minute, mYear,mMonth, mDay;
    static final int DATE_DIALOG_ID = 1;

    private String[] arrMonth = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
 private EditText txtDate;
 private EditText txtDate2;

 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_data);

    txtDate         = (EditText) findViewById(R.id.Date1);
    txtDate2        = (EditText) findViewById(R.id.Date2);

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

    txtDate.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             showDateDialog(txtDate, Calendar.getInstance());
            return true;

        }
    });

    txtDate2.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             showDateDialog(edTgl_akreditasi, Calendar.getInstance());
            return true;

        }
    }); 
  }

  EditText activeDateDisplay;
  Calendar activeDate;


   public void showDateDialog(EditText dateDisplay, Calendar date) 
    {
       activeDateDisplay = dateDisplay;
       activeDate = date;
       showDialog(DATE_DIALOG_ID);
    }
   @Override
   protected Dialog onCreateDialog(int id)
   {
      switch (id) 
       {
            case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
       }
    return null;
  }

 private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
  {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) 

        {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String sdate = arrMonth[mMonth] + " " + LPad(mDay + "", "0", 2) + ", " + mYear;
            txtDate.setText(sdate); //until here txtDate can get a value from the datetime picker, but how to set txtDate2 to get value from different datetimepicker?

        }
};

I really need help., Thank u

役に立ちましたか?

解決

Just change txtDate.setText(sdate); to activeDateDisplay.setText(sdate); . Also in ontouchlistener of editdate2, i dont know what edTgl_akreditasi is but i think it should be showDateDialog(editdate2, Calendar.getInstance()); instead of showDateDialog(edTgl_akreditasi, Calendar.getInstance());

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top