如何在其他Java类中访问我在MainActivity.java中声明的spinner对象。

huangapple go评论71阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2020年9月2日 02:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693245.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定