在Android Studio java类中声明按钮的最佳方法是什么?例如,我有按钮&文字字段:
TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
Button next_test = (Button)findViewById(R.id.Next_Text);
Button b = (Button)findViewById(R.id.button);
当我在类下面声明它们时,Android Studio会抛出错误:
public class main extends AppCompatActivity {
TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
Button next_test = (Button)findViewById(R.id.Next_Text);
Button b = (Button)findViewById(R.id.button);
@Override
protected void onCreate(Bundle savedInstanceState) {..........
当我在需要的方法中声明它们时,它不会抛出错误,但是我必须多次声明它们并在oncreate中声明final。
有没有办法只宣告一次?
答案 0 :(得分:2)
您的成员变量应该可以在您的班级内全局访问。但如果xml没有膨胀,你就无法通过id找到一个视图。这就是你的代码应该是这样的:
public class main extends AppCompatActivity {
TextView t;
TextView A;
TextView B;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.your_layout); // THIS IS VERY IMPORTANT
t = (TextView) findViewById(R.id.Question);
A = (TextView) findViewById(R.id.AnswerA);
B = (TextView) findViewById(R.id.AnswerB);
....
}
方法findViewById
在R.layout.your_layout中查找您的视图...如果您不调用setContentView()
,它将找不到任何内容并返回null。希望它有意义:))
答案 1 :(得分:1)
申报意见的正确方法:
columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()});
这适用于所有视图,如button,imageview,linearlayout,imageview等....
答案 2 :(得分:1)
你可以这样做
public class main extends AppCompatActivity {
private TextView t;
private TextView A;
private TextView B;
private TextView C;
private TextView D;
private Button next_test;
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize(){
t = (TextView) findViewById(R.id.Question);
A = (TextView) findViewById(R.id.AnswerA);
B = (TextView) findViewById(R.id.AnswerB);
C = (TextView) findViewById(R.id.AnswerC);
D = (TextView) findViewById(R.id.AnswerD);
next_test = (Button)findViewById(R.id.Next_Text);
b = (Button)findViewById(R.id.button);
}
答案 3 :(得分:1)
在OnCreate
方法
public class main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
TextView t = (TextView) findViewById(R.id.Question);
TextView A = (TextView) findViewById(R.id.AnswerA);
TextView B = (TextView) findViewById(R.id.AnswerB);
TextView C = (TextView) findViewById(R.id.AnswerC);
TextView D = (TextView) findViewById(R.id.AnswerD);
Button next_test = (Button)findViewById(R.id.Next_Text);
Button b = (Button)findViewById(R.id.button);
.....
}
您应该始终在onCreate
方法中初始化它们并放在
setContentView(R.layout.your_activity_layout);
答案 4 :(得分:-1)
您必须在onCreate
中声明您的观看次数。将findViewById
方法放在onCreate
。