使用非活动类,该类应能够处理调用活动的实例变量 [英] Working with a non-activity class, which should be able to handle the instance variable of the calling activity
问题描述
我有两个活动A和B,一个非活动类C.
I have two activities A and B, and a non activity class C.
要使用C的方法,我从当前正在运行的活动中创建C的实例c(例如A在前台,而A已创建C).
To use the methods of C, I create an instance c of C from the currently running activity (say A is in the foreground, and A has created C).
现在,我想使用c中活动A的实例变量.我该怎么办?
Now, I want to use the instance variables of activity A from c. What should I do?
我正在尝试将非活动C类用于多个活动.请帮我!我已经研究了很多,但是仍然找不到任何有用的东西. :(
I am trying to use the non activity C class for multiple activities. Please help me! I have researched it a lot, but still couldn't find anything useful about it. :(
这是示例代码:
ActivityOne.java:
ActivityOne.java:
package com.example.vaibhav.a21matchsticks;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Example extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// Now comes the method, I want to use in the non-activity java class.
public void buttonClick(View view) {
EditText editText = (EditText) findViewById(R.id.editMe); //finds the view from the activity
int i = Integer.parseInt(editText.getText().toString());
//Below code changes the view state, if the condition is true
if(i<4) {
findViewById(R.id.button).setEnabled(false);
}
}
假定xml文件具有线性布局,该布局具有一个EditText视图(标识为"editMe")和一个按钮(标识为"button").这样的活动不止1个,每个活动至少具有上述两个视图(并且可能更多).
Assume the xml file to have a linear layout having one EditText view (with id 'editMe') and a Button (with id 'button'). There are more than 1 activities like this, each having at least the above two views (and may be more).
我的工作是创建一个非活动类C,以处理不同活动的条件语句并相应地更改各自的布局.
My job is to create a non activity class C, to handle the conditional statement for different activities and change the respective layout accordingly.
推荐答案
尽管C语言很模糊,但您的C类似乎是一个帮助器类.现在,由于您要访问C类中的实例变量(可能还有其他信息),因此需要手动传递它们.根据传递的内容,可以在C类的构造函数中或通过C类本身的实例方法将其提供给C类.
Even though a but vague, your Class C seems to be a helper class. Now, since you want to access instance variables (and possibly other information) in your C class, you need to pass them manually. Depending on what you are passing, it can be provided to Class C either in its constructor or via instance methods of the Class C itself.
例如:基于问题的最后一个陈述,您可以通过以下方式将ViewGroup
引用从您的活动传递给C类:
For eg: based on the last statement of your question you can pass a ViewGroup
reference from your activity to Class C via something like this:
public void initLayout(ViewGroup parent) {
// Hide/unhide/perform actions/etc based on current activity needs
}
类似地,您也可以将enum
值传递给Class C,以区分您在Class C中的活动.对于Class C与活动的通信,您可以查看Java中的interface
.希望这会有所帮助.
Similarly, you can also pass an enum
value to Class C to differentiate between your activities in Class C. For a Class C to activity communication, you can look at interface
in Java. Hope this helps.
这篇关于使用非活动类,该类应能够处理调用活动的实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!