代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imaga=(ImageButton)findViewById(R.id.imageButton1);
imaga.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进入游戏", Toast.LENGTH_SHORT).show();
}
});
重点:
在编写xml文件时调用:
android:src="@drawable/button_state"
button_state.xml文件中的内容是:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/start_b"/>
<item android:state_pressed="false" android:drawable="@drawable/start_a"/>
</selector>
效果: