简体   繁体   中英

How to create a TimePicker from class which do not extends Activity, Android?

I'm trying to create TimePicker dialog from a class that doesnt extends the Activity class. I need TimePicker in a class called EnterTime which has Context attribute containing Activity Context.

All examples of TimePicker on the WEB are basic ones written in Activity class and that's why they confusing me.

I want time picker which works good on Android lower then 3.0. I don't use XML layout.

So basically I need suggestion where to put onCreateDialog() , how to retrieve chosen time, etc.

I've EditText which have onClickListener() for calling a TimePicker.

may be this one help you

public class MyTimePicker {
TimePickerDialog mTimePickerDialog;

public interface onTimeSet {
    public void onTime(TimePicker view, int hourOfDay, int minute);
}

onTimeSet mOnTimeSet;

public void setTimeListener(onTimeSet onTimeset) {
    mOnTimeSet = onTimeset;
}

public MyTimePicker(Context ctx) {
    mTimePickerDialog = new TimePickerDialog(ctx, new OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mOnTimeSet.onTime(view, hourOfDay, minute);

        }
    }, 1, 1, true);
}

public void show() {
    mTimePickerDialog.show();
}

and call this

public void ShowTimePicker() {
    MyTimePicker myTimePicker = new MyTimePicker(this);
    myTimePicker.show();
    myTimePicker.setTimeListener(new onTimeSet() {

        @Override
        public void onTime(TimePicker view, int hourOfDay, int minute) {
            Toast.makeText(MainActivity.this,
                    "time is " + hourOfDay + ":" + minute,
                    Toast.LENGTH_LONG).show();

        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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