help me to do this, i have button image it should be unselected,when i click it should be selected if i go next activity and if i come back it should be selected,if i was not select开发者_运维问答ed before going next activity it should be unselected.how can i do this i have 32 buttons totally in activity 1.
dwn15=(Button)findViewById(R.id.adultdwn15);
dwn15.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
if(teeth[30]==0){
dwn15.setBackgroundResource(R.drawable.adultdwn15);
teeth[30]=31;
}
else{
dwn15.setBackgroundResource(R.drawable.adultdwn15_pressed);
teeth[30]=0;
}
}
});
Try selector like this:
<?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/selectable_image" />
<item android:state_focused="true" android:drawable="@drawable/selectable_image" />
<item android:state_selected = "true" android:drawable = "@drawable/selectable_image" />
<item android:drawable = "@drawable/default_image" />
</selector>
set this selector in place of image to your button and on button click put button.setSelected("true")
.
You could prevent your activities from entering onCreate() every time you pass to another activity and come back. This can be done from manifest with the help of launchMode. Here is a link.
Another way to implement this is to keep an array of boolean for your buttons where you keep if a button was selected.
精彩评论