如何定义从另一个类调用 MainActivity 的方法?

如何定义从另一个类调用 MainActivity 的方法?

问题描述:

我想在我创建的另一个类的 MainActivity 中定义一个方法.我在弄清楚这一点时遇到了问题.这甚至可能吗?我在网上找不到关于在另一个类中定义方法的任何信息.

I want to define a method that I have in MainActivity in another class that I created. I am having issues figuring this out. Is this even possible? I am not finding anything online about defining a method in another class.

我已经包含了我的代码和我想如何做的例子.

I have included my code and the example of how I want to do it.

我的主活动代码

package com.example.flashcards;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    DatabseHelper DB = new DatabseHelper(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        changeText();
        changeText2();
    }

    private void changeText() {
        final String[] revertText = {"H2O", "What elements does water consist of?"};

        final TextView textChange = (TextView) findViewById(R.id.flashcard1);
        Button change = (Button) findViewById(R.id.answer1);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }

我的 TextC 代码(其他类)

My TextC code(other class)

package com.example.flashcards;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TextC extends MainActivity {
    private void changeText2() {
        final String[] revertText = {"2,200° F", "How hot does lava get?"};

        final TextView textChange = (TextView) findViewById(R.id.flashcard2);
        Button change = (Button) findViewById(R.id.answer2);

        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int random = (int) (Math.random() * revertText.length);
                textChange.setText(revertText[random]);
            }
        });
    }
}

我在 MainActivity 中定义的方法有效,但我似乎无法从我的其他类中得到它.我正在尝试做的可能吗?我想要在我的 TextC 类中定义 changeText2() 方法,但在我的 MainActivity 中声明,因为 MainActivity 有 onCreate() 方法.

My method defined in the MainActivity works, but I cannot seem to get it to work from my other class. Is what I am trying to do possible? I want changeText2() method defined in my TextC class, but declared in my MainActivity since the MainActivity has the onCreate() method.

尝试检查 OOP 概念的继承和覆盖.由于 MainAcitivity 是父类和 TextC 子类,因此您可以访问子类中的父函数,而不是其他方式.除非您弄清楚您正在寻找的确切结构,否则您尝试做的事情是不可能的.

Try checking OOPs concept for inheritance and overriding. As MainAcitivity is a parent class and TextC child, you get access of parent functions in child and not other way around. what you are trying to do is impossible unless you figure out the exact structure you are looking for.

您可能希望在 MainActicity 中将 changeText2() 函数声明为 public 或 protected,然后在需要时覆盖其在子类中的实现.

May be you are looking to declare changeText2() function as public or protected in MainActicity and then override its implementation in child class if needed at all.