[英]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. 我想对Android标准的
TimePicker
类进行一些更改。 Specifically, I'm trying to change it so it works in 15 minute increments, rather than 1 minute increments. 具体来说,我正在尝试对其进行更改,以使其以15分钟为增量而不是1分钟为增量。
This post helped me constrain the range of minute values to {0, 15, 30, 45}, as required in my app. 这篇文章帮助我按照应用程序的要求将分钟值的范围限制为{0,15,30,45}。 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.
但是,正如我在后续评论中指出的那样,分钟微调器仍将前一分钟显示为当前值-1,将下一分钟显示为当前值+ 1,这将创建一个马虎的用户界面。
I looked into the relevant Android source code , and it appears that the changes I would need to make are pretty simple. 我查看了相关的Android源代码 ,看来我需要进行的更改非常简单。 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. 但是,当我尝试将源代码复制到我的项目中时,遇到了与
package
声明,在哪里找到Widget
,如何解析R.id
变量等有关的无数错误。
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? 从Android源代码对给定类进行少量更改并将其合并到您自己的项目中的最佳方法是什么?
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. 就我而言,我只需要对
TimePicker
和NumberPicker
进行一些小的更改,但是我不确定如何在我的项目中进行适当的设置。
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. 并且由于您无法覆盖
android.widget.TimePicker
,因此您将需要将该类移动到新的包中,或者给它一个新的名称。
where to find Widget
在哪里找到小部件
That implies that you copied TimePicker
into one of your packages. 这意味着您将
TimePicker
复制到了一个程序包中。 That is fine, but then you need to add in the appropriate import
statements for classes that TimePicker
referred to from its original package. 很好,但是随后您需要为
TimePicker
从其原始包中引用的类添加适当的import
语句。 Or, you need to keep your (renamed) TimePicker
in android.widget
, adding this package to your project. 或者,您需要将(重命名的)
TimePicker
保留在android.widget
,将此包添加到您的项目中。 This is rudimentary Java. 这是基本的Java。
how to resolve R.id variables
如何解析R.id变量
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. 如果
TimePicker
依赖于不属于Android SDK的资源,则还需要将这些资源从AOSP复制到项目中。
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?
从Android源代码对给定类进行少量更改并将其合并到您自己的项目中的最佳方法是什么?
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: 在Java中,您可以在子类中执行以下操作:
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super. 您可以编写一个隐式或使用关键字super调用超类的构造函数的子类构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.