简体   繁体   English

如何对Android源代码进行少量更改并将其合并到您自己的项目中

[英]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. 就我而言,我只需要对TimePickerNumberPicker进行一些小的更改,但是我不确定如何在我的项目中进行适当的设置。

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中,您可以在子类中执行以下操作:

  • 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. 您可以编写一个隐式或使用关键字super调用超类的构造函数的子类构造函数。

    More info on subclassing in Java 有关Java子类化的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将开源JniLibs整合到自己的android项目中 - Incorporate open source JniLibs into own android project 如何制作自己的地图以便在Android上应用? - How to make your own map for application on Android? 如何使用预装的自定义应用程序(使用我自己的发布密钥)制作 android 开源项目 (AOSP)? - How to make android open source project (AOSP) with preinstalled custom apps (using my own release key)? 如何使用/合并CPP文件到Android项目? - How to use/incorporate CPP files to Android project? 如何将现有的make文件与Android NDK合并 - How to incorporate existing make file with Android NDK 如何在aosp中更改源代码并在其上添加我们自己的应用程序 - How to change source code in aosp and add our own application on it 如何从2个项目的Android合并源代码 - How to merge source code from 2 project Android 在自己的Android设备上构建并安装Android源代码? - build and install Android source on your own Android device? 如何将Android应用程序项目合并到Java项目中-Eclipse - How to incorporate an Android Application Project onto a Java Project - Eclipse Android:如何在Android中加密自己的数据库 - Android: How to encrypt your own database in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM