Android - 如果使用 setVisibility 如何完成按钮

发布于 2024-12-07 06:44:47 字数 904 浏览 2 评论 0原文

我有按钮,它们都完成后进入下一个活动。

但我正在尝试完成一个具有 setVisibility 的按钮,如下面的示例代码。

Button failfiveButton = (Button)findViewById(R.id.failfive);

failfiveButton.setOnClickListener(new OnClickListener() {

     public void onClick(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();
     }
});

因为上面的代码我有大约六个按钮,如果我添加 finish();mSoundManager.playSound(2);下,它返回到第一个.主屏幕。而不是让我继续并让其余按钮不可见。

如果我没有完成();应用程序因内存泄漏过多而滞后。

谢谢

瓦希德

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 onClick(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-12-14 06:44:47

之所以会发生这种情况,是因为单击按钮时就会调用此方法。它并不关心你点击了多少个按钮。在这里,您将完成每次单击按钮的活动,因此正在发生这种情况。一个愚蠢的解决方案可能是这样的

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();
        }
        });

编辑:可能还有其他更好的方法来做到这一点。但我给了你这个解决方案,因为我不知道你的目的到底是什么。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文