How to define a method called in MainActivity from another class?

微笑、不失礼 提交于 2020-02-06 15:45:14

问题


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.

My MainActivity Code

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]);
            }
        });
    }

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]);
            }
        });
    }
}

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.


回答1:


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.

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.




回答2:


TextC class should be your parent class of the MainActivity like

public class MainActivity extends TextC 

meanwhile TextC should be extend to AppCompactActivity and it should be abstact like shown in below.

public abstract class TextC extends AppCompatActivity 

if you do like this then you can access to functions present in TextC and change the name of the TextC and make it as BaseClass or something to make sense



来源:https://stackoverflow.com/questions/58601370/how-to-define-a-method-called-in-mainactivity-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!