[1] About the rewritten Class (here by Numberpciker as an example)
public class RakurakuNumberPicker extends NumberPicker {//[1]继承想要重写的控件
//[2]必须完成其自身的构造函数(三个都写上最好)
public RakurakuNumberPicker(Context context) {
super(context);
}
public RakurakuNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RakurakuNumberPicker(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//[3]贴上你自己添加的方法or重写的方法
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
SettingMessageNotificationTimeActivity.getInstance()
.updateArrowGuidanceUI(true);
}
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
SettingMessageNotificationTimeActivity.getInstance()
.updateArrowGuidanceUI(false);
}
return super.dispatchKeyEvent(event);
}
}
[2] The method at the call site
- A reference to the XML file (note the pose)
<com.android.mms.ui.RakurakuNumberPicker
android:id="@+id/notice_time_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
- Class file usage
//声明
private RakurakuNumberPicker mNoticeTimePicker;
//绑定
mNoticeTimePicker = (RakurakuNumberPicker)findViewById(R.id.notice_time_picker);
[3] Easter eggs (overridden View calls the method at the caller)
- Small episode, because static methods are required, all methods or members involved need to be static
This is clearly unacceptable
The final best solution
- Activity exposes a static method to get its object
protected static SettingMessageNotificationTimeActivity sMe;
public static SettingMessageNotificationTimeActivity getInstance() {
return sMe;
}
- The custom space gets the Activity object and then calls its method
SettingMessageNotificationTimeActivity.getInstance().FunctionTest();