嘿,我想知道您是如何做到的,以便在按下图像按钮时它会更改颜色以显示它已被按下。
DragonFruitActivity.java
package com.Dragon_Fruit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setBackgroundResource(R.drawable.playbuttonselected);
// TODO Auto-generated method stub
startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
}
private void setBackgroundResource(int playbuttonselected) {
// TODO Auto-generated method stub
}
});
ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
}
});
}
}
因此,新的活动应为:
package com.Dragon_Fruit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class DragonFruitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.setBackgroundResource(R.drawable.playbuttonselected);
// TODO Auto-generated method stub
startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
}
});
ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); settingsbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(DragonFruitActivity.this, settingsbutton.class));
}
});
}
}
最佳答案
在您的onClick(...)方法中,您可以setBackgroundResource(...)
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View arg0) {
setBackgroundResource(...);
startActivity(new Intent(DragonFruitActivity.this, playbutton.class));
}
}
(您还需要定义drawable用作背景色)。