gpt4 book ai didi

java - 引用函数控制

转载 作者:行者123 更新时间:2023-12-01 12:40:06 24 4
gpt4 key购买 nike

您必须耐心等待我,因为我确信这个问题一直被问到,但我没能找到它。

如果我有一个最小的 Android 应用程序,并且我似乎必须在 onCreate 这样的事件中声明所有控件,那么我如何在函数中引用这些控件,因为这些控件在应用程序“创建”之前不会初始化?

public class MyActivity extends ActionBarActivity {

public void myFunction(){
myButton.setText("Java is hard")
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final button myButton = (Button) findViewById(R.id.designerbutton);
}

每次调用函数时都必须将控件作为参数传递吗?这看起来有点笨拙。

最佳答案

了解 Member variables :

There are several kinds of variables:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.

The Bicycle class uses the following lines of code to define its fields:

public int cadence;
public int gear;
public int speed;

Field declarations are composed of three components, in order:

  • Zero or more modifiers, such as public or private.
  • The field's type.
  • The field's name.

The fields of Bicycle are named cadence, gear, and speed and are all of data type integer (int). The public keyword identifies these fields as public members, accessible by any object that can access the class.

关于java - 引用函数控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com