我在测试中有5个选择。我有一个csv数据库,我读了标题和答案。当我按下其中一个按钮时,我希望按钮上的文本更改为下一个标题。我想使用" for"为此循环。我将如何保持相同的布局,但更改此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);
}
});
}
}
答案 0 :(得分:0)
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]);
}
});
答案 1 :(得分:0)
感谢@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]);
}
});