如何在单独的函数中访问变量-Android [英] How to access variables in separate functions - Android

查看:98
本文介绍了如何在单独的函数中访问变量-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这个小应用程序,并且运行良好.但是我是java的新手,并且认为必须有一种更好的方法来编写此代码,以便可以在两个函数中读取变量.有吗?

I have written this small app and it works perfectly. But I am new to java and assume there must be a better way to write this so that the variables can be read in both functions. Is there?

package max.multiplebuttons.com;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class multibuttons extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView question = (TextView)findViewById(R.id.question);
        TextView textView = (TextView)findViewById(R.id.textView);
        Button answer1 = (Button)findViewById(R.id.answer1);
        Button answer2 = (Button)findViewById(R.id.answer2);
        answer1.setText("button1");
        answer2.setText("button2");
        question.setText("click a button");
        textView.setText("Some Text");
        answer1.setOnClickListener(this);
        answer2.setOnClickListener(this);
    }
    public void onClick(View v){
        TextView textView = (TextView)findViewById(R.id.textView);
        Button answer1 = (Button)findViewById(R.id.answer1);
        Button answer2 = (Button)findViewById(R.id.answer2);
        if(v==answer1){
            textView.setText("1");
        }
        if(v==answer2){
            textView.setText("2");
        }
    }
}

推荐答案

通过在类之外的任何方法之外声明它们,使它们属于该类的变量:

Make them variables that belong to the class by declaring them outside of any method but inside the class:

public class multibuttons extends Activity implements OnClickListener {
TextView question;
TextView textview;
//etc.

}

然后,您只需要在onCreate方法中对其进行初始化:

Then you just need to initialise them inside the onCreate method:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    question = (TextView)findViewById(R.id.question);
    textView = (TextView)findViewById(R.id.textView);
    //...

您完全不需要在onClick方法中再次初始化它们:

You don't need to initialise them again at all in the onClick method:

public void onClick(View v){
    if(v==answer1){
        textView.setText("1");
    }
    if(v==answer2){
        textView.setText("2");
    }
}

在方法(或用{}等大括号括起来的任何语句块)内声明的变量仅在该方法/块内具有范围(即,它们仅可见).可以将声明为类变量的变量指定为public,private,protected或default/package范围.将它们声明为公共对象,以便可以在任何其他班级使用它们.

Variables declared inside a method (or any block of statements enclosed by braces like {} ) only have scope (i.e. they are only visible) inside that method/block. Variables declared as class variables can be given public, private, protected or default/package scope. Declare them as public to be able to access them in any other class.

这篇关于如何在单独的函数中访问变量-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆