1

I have attached my code below. i want to get dialogue of Date picker on touch event event of Edit text. i am getting no error but also do not getting any Date dialogue.

Please Help me where i am wrong. Thanx in Advance.

String TAG = "AdvancedSearchActivity";
EditText txtArrCity, txtDepCity, txtDate, txtTime;
public String year, month, day;
public static final int DATE_DIALOG_ID = 1;
// OnDateSetListener dateListener;
String DATE;

/*
 * (non-Javadoc)
 * 
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.advance_search);

    txtArrCity = (EditText) findViewById(R.id.edittxt_ArrCity);
    txtDepCity = (EditText) findViewById(R.id.edittxt_DepCity);
    txtDate = (EditText) findViewById(R.id.edittxt_Date);
    txtTime = (EditText) findViewById(R.id.edittxt_Time);

    Calendar cal = Calendar.getInstance();
    year = String.valueOf(cal.get(Calendar.YEAR));
    month = String.valueOf(cal.get(Calendar.MONTH));
    day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));

    Log.i(TAG, "Year" + year);
    Log.i(TAG, "month" + (month + 1));
    Log.i(TAG, "Day" + day);
    updateDate();

    txtDate.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG_ID);
            Log.i(TAG, "Inside On touch");
            return false;
        }
    });

}

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(TabSample.tabcontext,
                dateListener, Integer.parseInt(year),
                Integer.parseInt(month), Integer.parseInt(day));

    }
    return null;

}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int years, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        year = String.valueOf(years);
        month = String.valueOf(monthOfYear);
        day = String.valueOf(dayOfMonth);
        updateDate();
    }
};

private void updateDate() {

    month = String.valueOf((Integer.parseInt(month) + 1));
    if ((Integer.parseInt(month)) < 10) {
        Log.i(TAG, "@@@@@ month" + month);
        month = "0" + month;
        Log.i(TAG, "@@@@@ AFTER month" + month + " MONTH S" + month);
    }
    day = String.valueOf(day);
    if ((Integer.parseInt(day)) < 10) {
        day = "0" + day;
    }
    DATE = year + "-" + month + "-" + day;

    Log.i(TAG, "DATE : " + DATE);
    txtDate.setText(DATE);
}
4

2 に答える 2

5

このようにしてみてください

 public class name extends Activity {   
     private EditText dob;
        static final int DATE_DIALOG_ID = 0;
        @Override
            protected Dialog onCreateDialog(int id) {
                Calendar c = Calendar.getInstance();
                int cyear = c.get(Calendar.YEAR);
                int cmonth = c.get(Calendar.MONTH);
                int cday = c.get(Calendar.DAY_OF_MONTH);
                switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
                }
                return null;
            }
            private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
                // onDateSet method
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    String date_selected = String.valueOf(dayOfMonth)+" /"+String.valueOf(monthOfYear+1)+" /"+String.valueOf(year);
                    //Toast.makeText(getApplicationContext(), "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
                    dob.setText(date_selected);
                }
            };
        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.bankregform);
            dob=(EditText)findViewById(R.id.dob);
        /* For DOB EditText*/
                dob.setOnTouchListener(new OnTouchListener(){ 
                    public boolean onTouch(View v, MotionEvent event) { 
                        if(v == dob)
                            showDialog(DATE_DIALOG_ID);
                        return false;              
                    }
                });
        }
    }
于 2012-05-23T11:00:22.293 に答える