android - 如何创建可重用的功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11603412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:12:33  来源:igfitidea点击:

android - how to create a reusable function?

android

提问by omega

In my android project, I have many activities and some of them already extend other stuff like map activity or BroadcastReceiver.

在我的 android 项目中,我有很多活动,其中一些活动已经扩展了其他内容,例如地图活动或广播接收器。

How do I create a function that I can call from any activity, because I don't want to have to repeat any code in multiple activities.

如何创建可以从任何活动调用的函数,因为我不想在多个活动中重复任何代码。

thanks.

谢谢。

回答by Sparky

If I have useful functions that perform little helpful tasks that I want to invoke from several Activities, I create a class called Util and park them in there. I make them staticso that I don't need to allocate any objects.

如果我有一些有用的函数来执行我想从多个活动中调用的一些有用的任务,我会创建一个名为 Util 的类并将它们放在那里。我制作它们static以便我不需要分配任何对象。

Here is an example of part of one such class I wrote:

这是我编写的此类类的一部分的示例:

public final class Util {
    public final static int KIBI = 1024;
    public final static int BYTE = 1;
    public final static int KIBIBYTE = KIBI * BYTE;

    /**
     * Private constructor to prevent instantiation
     */
    private Util() {}

    public static String getTimeStampNow() {
        Time time = new Time();
        time.setToNow();
        return time.format3339(false);
    }
}

To use these constants and methods, I can access them from the class name, rather than any object:

要使用这些常量和方法,我可以从类名而不是任何对象访问它们:

int fileSize = 10 * Util.KIBIBYTE;
String timestamp = Util.getTimeStampNow();

There's more to the class than this, but you get the idea.

课堂上还有比这更多的东西,但你明白了。

回答by Dimse

You can extend the Application class, then in your activities call the getApplication method and cast it to your application class in order to call the method.

您可以扩展 Application 类,然后在您的活动中调用 getApplication 方法并将其转换为您的应用程序类以调用该方法。

You do this by creating a class that extends android.app.Application:

您可以通过创建一个扩展 android.app.Application 的类来做到这一点:

package your.package.name.here;

import android.app.Application;

public class MyApplication extends Application {

    public void doSomething(){
        //Do something here
    }
}

In your manifest you must then find the tag and add the android:name="MyApplication" attribute.

在您的清单中,您必须找到标签并添加 android:name="MyApplication" 属性。

In your activity class you can then call the function by doing:

在您的活动类中,您可以通过执行以下操作来调用该函数:

((MyApplication)getApplication()).doSomething();

There are other ways of doing something similar, but this is one of the ways. The documentation even states that a static singleton is a better choice in most cases. The Application documentation is available at: http://developer.android.com/reference/android/app/Application.html

还有其他方法可以做类似的事情,但这是其中一种方法。文档甚至指出在大多数情况下静态单例是更好的选择。应用程序文档位于:http: //developer.android.com/reference/android/app/Application.html

回答by Luke Taylor

You could create a static method or an object that contains this method.

您可以创建静态方法或包含此方法的对象。

回答by Shachar Shemesh

You can create a class extending Activity, and then make sure your real activities are subclasses of that activity, instead of the usual built-in one. Simply define your common code in this parent activity.

您可以创建一个扩展 Activity 的类,然后确保您的真实活动是该活动的子类,而不是通常的内置活动。只需在此父活动中定义您的通用代码。

Shachar

沙查尔

回答by myworldbox

  1. Create a new Java class BaseActivity with abstract Modifiers and extends it with AppCompatActivity.

  2. Move all your methods under Java class BaseActivity.

  1. 使用抽象修饰符创建一个新的 Java 类 BaseActivity 并使用 AppCompatActivity 对其进行扩展。

  2. 将所有方法移到 Java 类 BaseActivity 下。

package com.example.madbox;

public abstract class BaseActivity extends AppCompatActivity {

    protected void YourClass() {

    }
}

  1. Extends your Activities with BaseActivity but not AppCompatActivity.
  1. 使用 BaseActivity 而不是 AppCompatActivity 扩展您的活动。