尝试制作一款儿童追逐大理石的游戏。我希望按钮背景只有在另一个按钮背景相等的情况下才会改变。但无法通过if语句获得onclick工作。这是我目前的代码。
package test.tablet.design;
import java.text.DateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MarbleGame extends Activity {
private Button btnA1mc; private Button btnA2mc; private Button btnA3mc; private Button btnA4mc; private Button btnB1mc; private Button btnB2mc; private Button btnB3mc; private Button btnB4mc;
private Button btnC1mc; private Button btnC2mc; private Button btnC3mc; private Button btnC4mc; private Button btnD1mc; private Button btnD2mc; private Button btnD3mc; private Button btnD4mc;
private Button btnE1mc; private Button btnE2mc; private Button btnE3mc; private Button btnE4mc; private Button btnF1mc; private Button btnF2mc; private Button btnF3mc; private Button btnF4mc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marble_chase);
TextView tv_time = (TextView) findViewById(R.id.tv_time);
String currenttime = DateFormat.getDateTimeInstance().format(new Date());
tv_time.setText(currenttime);
btnA1mc = (Button)findViewById(R.id.btnA1mc); btnA2mc = (Button)findViewById(R.id.btnA2mc); btnA3mc = (Button)findViewById(R.id.btnA3mc); btnA4mc = (Button)findViewById(R.id.btnA4mc);
btnB1mc = (Button)findViewById(R.id.btnB1mc); btnB2mc = (Button)findViewById(R.id.btnB2mc); btnB3mc = (Button)findViewById(R.id.btnB3mc); btnB4mc = (Button)findViewById(R.id.btnB4mc);
btnC1mc = (Button)findViewById(R.id.btnC1mc); btnC2mc = (Button)findViewById(R.id.btnC2mc); btnC3mc = (Button)findViewById(R.id.btnC3mc); btnC4mc = (Button)findViewById(R.id.btnC4mc);
btnD1mc = (Button)findViewById(R.id.btnD1mc); btnD2mc = (Button)findViewById(R.id.btnD2mc); btnD3mc = (Button)findViewById(R.id.btnD3mc); btnD4mc = (Button)findViewById(R.id.btnD4mc);
btnE1mc = (Button)findViewById(R.id.btnE1mc); btnE2mc = (Button)findViewById(R.id.btnE2mc); btnE3mc = (Button)findViewById(R.id.btnE3mc); btnE4mc = (Button)findViewById(R.id.btnE4mc);
btnF1mc = (Button)findViewById(R.id.btnF1mc); btnF2mc = (Button)findViewById(R.id.btnF2mc); btnF3mc = (Button)findViewById(R.id.btnF3mc); btnF4mc = (Button)findViewById(R.id.btnF4mc);
btnE2mc.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
btnE2mc.setBackgroundResource(R.drawable.marble_x);
btnB4mc.setBackgroundResource(R.drawable.marble);
}
});
btnB4mc.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (btnE2mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnB4mc.setBackgroundResource(R.drawable.marble_x);
btnC1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
btnC1mc.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)))
{
btnC1mc.setBackgroundResource(R.drawable.marble_x);
btnF1mc.setBackgroundResource(R.drawable.marble);
}
else{}
}
});
}
}
答案 0 :(得分:0)
只要单击按钮并更改背景,我就会将内部boolean
设置为true。
boolean isBackgroundChanged = false;
每当点击该按钮时,您都会更改背景并将isBackgroundChanged
设置为true
。
然后,而不是做:
btnB4mc.equals(getResources().getDrawable(R.drawable.marble_x)
你会有
if(isBackgroundChanged)...
答案 1 :(得分:0)
您正在尝试将Button对象与Drawable对象进行比较,该对象将始终返回false。
您可能希望使用View.setTag()来跟踪每个按钮的状态。使用标记(命名或未命名),您可以将任意数据与单个视图相关联。在下面的示例中,自动装箱用于将布尔值转换为布尔值。
if ((Boolean)btn1.getTag()) {
btn1.setTag(false);
btn2.setTag(true);
// TODO actual background swapping
}
为了便于阅读和减少重复,您可能希望使用可以为您进行交换的辅助函数,这将使用两个按钮作为参数。