Onclicklistener上的对话框

时间:2013-12-17 07:16:08

标签: android dialog timepicker

到目前为止没有错误,但是当我单击EditText1时没有出现对话框。

public class MainActivity extends Activity {
static final int dialog_id = 0;
int hour,minutes;
Button date;
Button time;
DatePicker dp;
TimePicker tp;
EditText et;
EditText et1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    date = (Button) findViewById(R.id.button1);
    time = (Button) findViewById(R.id.button2);
    et = (EditText) findViewById(R.id.editText1);
    et1 = (EditText) findViewById(R.id.EditText01);

    et.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            date();

        }
    });

    et1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false);
        }
    });

}  


public void date(){
    dp = new DatePicker(this);
    new AlertDialog.Builder(this)
    .setTitle("Set Date")
    .setView(dp)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), ""+dp.getMonth()+1+"-"+dp.getDayOfMonth()+"-"+dp.getYear(), Toast.LENGTH_LONG).show();
            et.setText(dp.getMonth()+1+"-"+dp.getDayOfMonth()+"-"+dp.getYear());
        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .show();

}

private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                // TODO Auto-generated method stub
                hour = hourOfDay;
                minutes = minute;
                String tme = hour +":"+tp.getCurrentMinute();
                et1.setText(tme);
            }
        };

这是一个日期和时间选择器对话框。 Datepicker有效,但时间戳没有。我没有对日期选择器做同样的事情,因为我无法改变时间的格式。

1 个答案:

答案 0 :(得分:0)

onCreate(Bundle savedInstanceState)

之后的et1 = (EditText) findViewById(R.id.EditText01);方法中添加以下行
        Calendar c=Calendar.getInstance();
        hour=c.get(Calendar.HOUR);
        minutes=c.get(Calendar.MINUTE);

在下面的行中写下true而不是false

new TimePickerDialog(MainActivity.this, mTimeSetListener, hour, minutes, false).show();

true 24小时格式。

并替换以下行

        String tme = hour +":"+tp.getCurrentMinute();
        et1.setText(tme);

et1.setText(hour+" : "+minutes);

希望它的工作。