[英]I have 5 choices in a test. I have a csv database. When I press on one of the buttons, I want the Text on the button to change to the next header
I have 5 choices in a test. 我在测试中有5个选择。 I have a csv database which I read the headers and answers from.
我有一个csv数据库,可从中读取标题和答案。 When I press on one of the buttons, I want the Text on the button to change to the next header.
当我按下其中一个按钮时,我希望按钮上的“文本”更改为下一个标题。 I want to use a "for" loop for that.
我想为此使用“ for”循环。 How will I keep the same layout, but change the text on the buttons in this for loop?
我如何保持相同的布局,但是在此for循环中更改按钮上的文本?
public class MainActivity extends AppCompatActivity {
int say,may=0;
Button a,b,c,d,e;
private List<WeatherSample> weatherSamples=new ArrayList<>();
String[][] deneme=new String[20][7];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button asikki = (Button) findViewById(R.id.asikki);
final Button bsikki = (Button) findViewById(R.id.bsikki);
final Button csikki = (Button) findViewById(R.id.csikki);
final Button dsikki = (Button) findViewById(R.id.dsikki);
final Button esikki = (Button) findViewById(R.id.esikki);
String line = "";
a = (Button) findViewById(R.id.asikki);
b = (Button) findViewById(R.id.bsikki);
c = (Button) findViewById(R.id.csikki);
d = (Button) findViewById(R.id.dsikki);
e = (Button) findViewById(R.id.esikki);
InputStream is = getResources().openRawResource(R.raw.data);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
try {
// step over header line
reader.readLine();
while ((line = reader.readLine()) != null) {
may++;
String sira = Integer.toString(may);
String[] tokens = line.split(",");
WeatherSample sample = new WeatherSample();
deneme[may][0] = tokens[0];
deneme[may][1] = tokens[1];
deneme[may][2] = tokens[2];
deneme[may][3] = tokens[3];
deneme[may][4] = tokens[4];
deneme[may][5] = tokens[5];
deneme[may][6] = tokens[6];
}
} catch (IOException e) {
e.printStackTrace();
}
for (int say=0;say<10;say++){
a.setText("A) " + deneme[1][0]);
b.setText("B) " + deneme[1][1]);
c.setText("C) " + deneme[1][2]);
d.setText("D) " + deneme[1][3]);
e.setText("E) " + deneme[1][4]);
asikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
asikki.setText("bilgin");
asikki.setBackgroundColor(Color.BLACK);
asikki.setTextColor(Color.WHITE);
}
});
bsikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bsikki.setText("bilgin");
bsikki.setBackgroundColor(Color.BLACK);
bsikki.setTextColor(Color.WHITE);
}
});
csikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
csikki.setText("bilgin");
csikki.setBackgroundColor(Color.BLACK);
csikki.setTextColor(Color.WHITE);
}
});
dsikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dsikki.setText("bilgin");
dsikki.setBackgroundColor(Color.BLACK);
dsikki.setTextColor(Color.WHITE);
}
});
esikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
esikki.setText("bilgin");
esikki.setBackgroundColor(Color.BLACK);
esikki.setTextColor(Color.WHITE);
}
});
}
}
for
loop is something to be executed immediately, it doesn't wait, you only need the listener for
循环是立即执行的操作,它不等待,您只需要侦听器
asikki.setText(deneme[0][0]);
asikki.setBackgroundColor(Color.BLACK);
asikki.setTextColor(Color.WHITE);
asikki.setOnClickListener(new View.OnClickListener() {
int say = 1;
public void onClick(View v) {
// TODO check say isn't out of bound
asikki.setText("A) " +deneme[say++][0]);
}
});
Thanks to @Happy, Now I have the following lines in my code: 感谢@Happy,现在我的代码中包含以下几行:
int say = 1;
asikki.setText(deneme[0][0]);
asikki.setBackgroundColor(Color.BLACK);
asikki.setTextColor(Color.WHITE);
asikki.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
say++;
asikki.setText("A) " +deneme[say][1]);
bsikki.setText("B) " +deneme[say][2]);
csikki.setText("C) " +deneme[say][3]);
dsikki.setText("D) " +deneme[say][4]);
esikki.setText("E) " +deneme[say][5]);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.