我目前正在Android Studio中进行问答游戏,但是我很难解决由按钮的OnClickListener内部类中的外部类变量引起的错误。我需要使用这些变量来为问题,选择和答案指定数组索引。
这是代码
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class info extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
String [] questionsIT = {
"When did Windows 10 appear?",
"When did the first iPhone appear?",
"Who presented the iPhone 4?",
"What's the most widely used browser in the world?",
"Which language is Windows written in?",
"WHich country did the inventor of PHP language come from?",
"How many cores does a quad-core processor have?",
"Which company owns XBOX?",
"Which company owns PlayStation?",
"In which american state are the Google headquarters located?"
};
String [][] choicesIT = {
{"1989", "2015", "2002", "2012"},
{"1999", "2006", "2008", "2007"},
{"Steve Jobs", "Tim Cook", "Bill Gates", "Steve Wozniak"},
{"Microsoft Edge", "Internet Explorer", "Google Chrome", "Mozzila Firefox"},
{"C++", "Pascal", "Java", "Perl"},
{"Romania", "UK", "Greenland", "Germany"},
{"2 cores", "4 cores", "6 cores", "8 cores"},
{"Google", "Sony", "Samsung", "Microsoft"},
{"Microsoft", "Sony", "Apple", "Nokia"},
{"New York", "Texas", "Ohio", "California"}
};
final String [] answersIT = {
"2015",
"2007",
"Steve Jobs",
"Google Chrome",
"C++",
"Greenland",
"4 cores",
"Microsoft",
"Sony",
"California"
};
int[] picsID = {R.drawable.win10, R.drawable.iphone1, R.drawable.apple};
RelativeLayout rlinfo = findViewById(R.id.rlinfo); //se alege layout si se init var pt intrb
TextView intrbinfo = findViewById(R.id.intrbinfo);
Button ch1 = findViewById(R.id.ch1);
Button ch2 = findViewById(R.id.ch2);
Button ch3 = findViewById(R.id.ch3);
Button ch4 = findViewById(R.id.ch4);
final Button[] chooses = new Button[]{ch1, ch2, ch3, ch4};
for(i = 0; i<10; i++)
{
intrbinfo.setText(questionsIT[i]);
chooses[0].setText(choicesIT[i][0]);
chooses[1].setText(choicesIT[i][1]);
chooses[2].setText(choicesIT[i][2]);
chooses[3].setText(choicesIT[i][3]);
chooses[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(chooses[0].getText().equals(answersIT[i]))
{
Right(v);
score++;
}
}
});
}
}
public void Right(View v)
{
}
public void Wrong(View v2)
{
}
}