我有四个图像按钮,我想以编程方式更改其背景,而无需使用onClickListener。
public class playactivity extends Activity {
private ImageButton first;
private ImageButton second;
private ImageButton third;
private ImageButton fourth;
private int stage1;
private database db1;
static boolean sw=false;
static boolean sw1=false;
static boolean sw2=false;
static int c1,c2,c3,c4=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playactivity);
first=(ImageButton)findViewById(R.id.firstbtn);
second=(ImageButton)findViewById(R.id.secbtn);
third=(ImageButton)findViewById(R.id.thirdbtn);
fourth=(ImageButton)findViewById(R.id.fourthbtn);
db1=new database(this);
db1.useable();
db1.open();
stage1=db1.newstage(1);
//stages of game
if(stage1 == 1)
second.setImageResource(R.drawable.unlock);
else if(stage1 == 2)
{
second.setImageResource(R.drawable.unlock);
third.setImageResource(R.drawable.unlock);
}
else if(stage1 == 3)
{
second.setImageResource(R.drawable.unlock);
third.setImageResource(R.drawable.unlock);
fourth.setImageResource(R.drawable.unlock);
}
first.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c1++;
}
});
second.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(sw){
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c2++;
// second.setImageResource(R.drawable.unlock); <--
}
}
});
third.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(sw1){
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c3++;
//third.setImageResource(R.drawable.unlock); <--
}
}
});
fourth.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(sw2){
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c4++;
// fourth.setImageResource(R.drawable.unlock); <--
}
}
});
}}
这段代码不起作用,但奇怪的是,我可以使用像这样的onClickListener更改按钮的背景:
second.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (sw) {
Intent intent = new Intent(PlayActivity.this, Main.class);
startActivity(intent);
c2++;
second.setImageResource(R.drawable.unlock);
}
}
});
third.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
if (sw1) {
Intent intent = new Intent(PlayActivity.this, Main.class);
startActivity(intent);
c3++;
third.setImageResource(R.drawable.unlock);
}
}
});
答案 0 :(得分:0)
这是我的问题的答案:)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playactivity);
first=(ImageButton)findViewById(R.id.firstbtn);
second=(ImageButton)findViewById(R.id.secbtn);
third=(ImageButton)findViewById(R.id.thirdbtn);
fourth=(ImageButton)findViewById(R.id.fourthbtn);
first.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c1++;
}
});
second.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c2++;
}
});
third.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c3++;
}
});
fourth.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Intent first1=new Intent(playactivity.this,Main.class);
startActivity(first1);
c4++;
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
db1=new database(this);
db1.useable();
db1.open();
stage1=db1.newstage(1);
if(stage1 == 1)
{
//Resources resources = getResources();
// second.setImageDrawable(resources.getDrawable(R.drawable.unlock));
second.setImageResource(R.drawable.unlock);
}
else if(stage1 == 2)
{
second.setImageResource(R.drawable.unlock);
third.setImageResource(R.drawable.unlock);
}
else if(stage1 == 3)
{
second.setImageResource(R.drawable.unlock);
third.setImageResource(R.drawable.unlock);
fourth.setImageResource(R.drawable.unlock);
}
}