Android - How to finish button if using setVisibility
I have buttons and they all finish to go to the next activity.
But I'm trying to finish an button that has setVisibility like example code below.
Button failfiveButton = (Button)findViewById(R.id.failfive);
failfiveButton.setOnClickListener(new OnClickListener() {
public void 开发者_如何学JAVAonClick(View v) {
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
mSoundManager.playSound(2);
finish();
}
});
Button failsixButton = (Button)findViewById(R.id.failsix);
failsixButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
mSoundManager.playSound(2);
finish();
}
});
Because I have about six buttons of the code above and if I add finish();
under mSoundManager.playSound(2);
it goes back to the first .main screen. Instead of letting me continue and make the rest of the buttons invisible.
If I don't finish(); the application lags from too much memory leaking.
Thanks
Wahid
Its happening because this method gets called when ever a button is clicked. it does't care how many buttons you click. here you are finishing your Activity for each button click, so this is happening. a silly solution could be this
private static int count = 0;
private static int count1 = 0;
failfiveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
mSoundManager.playSound(2);
if(count == 6)
finish();
}
});
Button failsixButton = (Button)findViewById(R.id.failsix);
failsixButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count1++
Button button = (Button) v;
button.setVisibility(View.INVISIBLE);
mSoundManager.playSound(2);
if(count1==6)
finish();
}
});
Edit: there could be other better ways to do it. but i gave you this solution since i don't know what exactly is your purpose.
精彩评论