我有一堆按钮,并分配了一些条款。我希望用户按照他们的意思连接它们。无论如何,我需要当用户按下按钮时,没有任何事情发生,只是可能为它设置颜色,并等到他按下另一个,然后采取一些行动(在我的情况下比较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();
}
}
答案 0 :(得分:0)
Button a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8;
//private boolean mShouldTakeAction; does not need this anymore
private Button mDisableButton;
.......
.......
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
// We are going to disable button so no need to make it yellow
//Button button = (Button)v;
//button.setBackgroundColor(Color.YELLOW);
if (mDisableButton == null)
{
mDisableButton = (Button) v;
v.setEnabled(false);
}
else
{
mDisableButton.setEnabled(true);
mDisableButton = null;
// do what ever you want here that is compare the terms
}
}
};