英文:
How to access spinner object which i have declared in MainActivity.java into other java class
问题
以下是您要的翻译内容:
我正在尝试对我的另一个类 extract.java 中的微调器(spinner)值执行一些操作,但我不知道如何从 MainActivity.java 访问微调器。
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = findViewById(R.id.spin1);
Spinner spin2 = findViewById(R.id.spin2);
Spinner spin3 = findViewById(R.id.spin3);
我的另一个 Java 类 extract.java
是否需要导入语句来实现这一点?
英文:
I am trying to do some operations on the spinner values in my other class extract.java
but, i am not aware on how to access spinners from MainActivity.java
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = findViewById(R.id.spin1);
Spinner spin2 = findViewById(R.id.spin2);
Spinner spin3 = findViewById(R.id.spin3);
My other java class extract.java
Are there any import statements to do this?
答案1
得分: 1
以下是要翻译的内容:
你可以像这样做:
public class Extract {
// 类构造函数
public Extract(Spinner spin1, Spinner spin2, Spinner spin3) {
// 在这里对你的下拉框进行操作,或者保存它们以供以后使用
}
}
在MainActivity中:
Spinner spin1 = findViewById(R.id.spin1);
Spinner spin2 = findViewById(R.id.spin2);
Spinner spin3 = findViewById(R.id.spin3);
Extract e = new Extract(spin1, spin2, spin3);
英文:
You can do something like this
public class Extract {
// class constructor
public Extract(Spinner spin1, Spinner spin2, Spinner spin3) {
//do something with your spinners or save them for later use
}
}
In MainActivity:
Spinner spin1 = findViewById(R.id.spin1);
Spinner spin2 = findViewById(R.id.spin2);
Spinner spin3 = findViewById(R.id.spin3);
Extract e = new Extract(spin1, spin2, spin3)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论