简体   繁体   中英

How to make a small change to Android source code, and incorporate into your own project

I want to make a small change to the Android standard TimePicker class. Specifically, I'm trying to change it so it works in 15 minute increments, rather than 1 minute increments.

This post helped me constrain the range of minute values to {0, 15, 30, 45}, as required in my app. But as I pointed out in a follow up comment, the minute spinner still shows previous minute as current value - 1, and the next minute as current value + 1, which creates a sloppy-feeling user interface.

I looked into the relevant Android source code , and it appears that the changes I would need to make are pretty simple. But when I tried copying the source code into my project I got about a zillion errors relating to the package declaration, where to find Widget , how to resolve R.id variables, etc.

So my question is:

What's the best way to make a small change to a given class from Android source code, and incorporate it into your own project?

In my case, I just need to make a few small changes to TimePicker and NumberPicker , but I'm not sure how to properly set this up in my project.

Thanks for any suggestions.

But when I tried copying the source code into my project I got about a zillion errors relating to the package declaration

Your source file's directory needs to match the package name. And since you cannot overwrite android.widget.TimePicker , you will either need to move that class to a new package or give it a new name.

where to find Widget

That implies that you copied TimePicker into one of your packages. That is fine, but then you need to add in the appropriate import statements for classes that TimePicker referred to from its original package. Or, you need to keep your (renamed) TimePicker in android.widget , adding this package to your project. This is rudimentary Java.

how to resolve R.id variables

If TimePicker relies upon resources that are not part of the Android SDK, you will need to copy those resources from the AOSP into your project as well.

What's the best way to make a small change to a given class from Android source code, and incorporate it into your own project?

IMHO, that cannot be answered readily in the abstract. Generally speaking, you do the sorts of things that I listed above.

You are best off subclassing the relevant classes and overriding the methods you would like to change.

In Java, you can do the following in a subclass:

  • The inherited fields can be used directly, just like any other fields.
  • You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
  • You can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can be used directly as they are.
  • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
  • You can declare new methods in the subclass that are not in the superclass.
  • You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

    More info on subclassing in Java

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