我有一堆按钮,并为它们分配了一些术语。我希望用户按其含义将它们连接起来。无论如何,我需要当用户按下一个按钮时,什么都不会发生,只是可能为它设置一个颜色,然后等到他按下另一个按钮,然后采取一些行动(在我的情况下,比较 setText 的术语纽扣)。同样,当他按下下一个按钮时,直到他按下第四个按钮,才会发生任何事情,依此类推。我怎样才能做到这一点?在这部分之前我已经完成了所有的工作,我已经导入了数据库,为我的按钮分配了文本等等。
编辑:
好的,就像斯蒂芬问的那样。我对所有按钮使用一个 onClickListener。我给他们分配了一个标签(我希望是正确的......我对正确的配对使用了相同的标签),稍后我将使用它来检查正确的答案(仍在学习)。我将按钮设置为在按下时更改它的颜色,现在我需要它等到我按下 ArrayList labelB 中的其他按钮。如果我按下 labelA 一组按钮中的一个按钮,我不想发生任何事情,只是改变颜色。也许你们认为“哇,这太简单了,太蹩脚了”,但现在对我来说已经很复杂了。:)
public class Game extends Activity implements View.OnClickListener{
// labelForButton and tagForButton
class MyStruct {
public MyStruct (String lab, String t){
label = lab;
tag = t;
}
private String label;
private String tag;
}
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
button.setBackgroundColor(Color.YELLOW);
}
};
Button a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
a1 = (Button) findViewById(R.id.bA1);
a2 = (Button) findViewById(R.id.bA2);
a3 = (Button) findViewById(R.id.bA3);
a4 = (Button) findViewById(R.id.bA4);
a5 = (Button) findViewById(R.id.bA5);
a6 = (Button) findViewById(R.id.bA6);
a7 = (Button) findViewById(R.id.bA7);
a8 = (Button) findViewById(R.id.bA8);
b1 = (Button) findViewById(R.id.bB1);
b2 = (Button) findViewById(R.id.bB2);
b3 = (Button) findViewById(R.id.bB3);
b4 = (Button) findViewById(R.id.bB4);
b5 = (Button) findViewById(R.id.bB5);
b6 = (Button) findViewById(R.id.bB6);
b7 = (Button) findViewById(R.id.bB7);
b8 = (Button) findViewById(R.id.bB8);
nextQuestion();
}
public void nextQuestion() {
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
try{
mDbHelper.open();
Cursor c = mDbHelper.getTestData(generateWhereClause());
mAnsweredQuestions.add(c.getLong(0));
ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>();
ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>();
labelsA.add(new MyStruct(c.getString(2), "1"));
labelsB.add(new MyStruct(c.getString(3), "1"));
labelsA.add(new MyStruct(c.getString(4), "2"));
labelsB.add(new MyStruct(c.getString(5), "2"));
labelsA.add(new MyStruct(c.getString(6), "3"));
labelsB.add(new MyStruct(c.getString(7), "3"));
labelsA.add(new MyStruct(c.getString(8), "4"));
labelsB.add(new MyStruct(c.getString(9), "4"));
labelsA.add(new MyStruct(c.getString(10), "5"));
labelsB.add(new MyStruct(c.getString(11), "5"));
labelsA.add(new MyStruct(c.getString(12), "6"));
labelsB.add(new MyStruct(c.getString(13), "6"));
labelsA.add(new MyStruct(c.getString(14), "7"));
labelsB.add(new MyStruct(c.getString(15), "7"));
labelsA.add(new MyStruct(c.getString(16), "8"));
labelsB.add(new MyStruct(c.getString(17), "8"));
Collections.shuffle(labelsA);
Collections.shuffle(labelsB);
pitanje.setText(c.getString(1));
a1.setText(labelsA.get(0).label);
a1.setTag(labelsA.get(0).tag);
a1.setOnClickListener(clickListener);
b1.setText(labelsB.get(0).label);
b1.setTag(labelsB.get(0).tag);
b1.setOnClickListener(clickListener);
a2.setText(labelsA.get(1).label);
a2.setTag(labelsA.get(1).tag);
a2.setOnClickListener(clickListener);
b2.setText(labelsB.get(1).label);
b2.setTag(labelsB.get(1).tag);
b2.setOnClickListener(clickListener);
a3.setText(labelsA.get(2).label);
a3.setTag(labelsA.get(2).tag);
a3.setOnClickListener(clickListener);
b3.setText(labelsB.get(2).label);
b3.setTag(labelsB.get(2).tag);
b3.setOnClickListener(clickListener);
a4.setText(labelsA.get(3).label);
a4.setTag(labelsA.get(3).tag);
a4.setOnClickListener(clickListener);
b4.setText(labelsB.get(3).label);
b4.setTag(labelsB.get(3).tag);
b4.setOnClickListener(clickListener);
a5.setText(labelsA.get(4).label);
a5.setTag(labelsA.get(4).tag);
a5.setOnClickListener(clickListener);
b5.setText(labelsB.get(4).label);
b5.setTag(labelsB.get(4).tag);
b5.setOnClickListener(clickListener);
a6.setText(labelsA.get(5).label);
a6.setTag(labelsA.get(5).tag);
a6.setOnClickListener(clickListener);
b6.setText(labelsB.get(5).label);
b6.setTag(labelsB.get(5).tag);
b6.setOnClickListener(clickListener);
a7.setText(labelsA.get(6).label);
a7.setTag(labelsA.get(6).tag);
a7.setOnClickListener(clickListener);
b7.setText(labelsB.get(6).label);
b7.setTag(labelsB.get(6).tag);
b7.setOnClickListener(clickListener);
a8.setText(labelsA.get(7).label);
a8.setTag(labelsA.get(7).tag);
a8.setOnClickListener(clickListener);
b8.setText(labelsB.get(7).label);
b8.setTag(labelsB.get(7).tag);
b8.setOnClickListener(clickListener);
}
finally{
mDbHelper.close();
}
}