我正在扩展android中的EditText类以包含其他功能,其中一个功能是在单击时显示对话框。我希望这种行为是可移植的,因此也是自包含的。
但是,将onClickListener
设置为自身(this)作为参数无效,并且永远不会调用函数onClick(View)
。
public class TimePickerEditText extends EditText implements View.OnClickListener, TimePickerDialog.OnTimeSetListener {
private Calendar today;
private TimePickerDialog timePickerDialog;
public TimePickerEditText(Context context) {
super(context);
postInstantiateSetup();
}
public TimePickerEditText(Context context, AttributeSet attrs) {
super(context, attrs);
postInstantiateSetup();
}
public TimePickerEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
postInstantiateSetup();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
postInstantiateSetup();
}
public void postInstantiateSetup()
{
setOnClickListener(this);
today = Calendar.getInstance();
onTimeSet(null,today.get(Calendar.HOUR_OF_DAY),today.get(Calendar.MINUTE));
}
@Override
public void onClick(View view) {
if(timePickerDialog == null) {
timePickerDialog = new TimePickerDialog(getContext(), this, 20, 0, true);
}
timePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker timePicker, int hours, int minutes) {
String hoursString = ""+hours;
if(hours<10)
hoursString="0"+hoursString;
String minutesString = ""+minutes;
if(minutes<10)
minutesString="0"+minutesString;
this.setText(hoursString+":"+minutesString);
}
}