安卓。如何使用 setClickable 同时设置所有按钮可点击或不可点击?

发布于 2024-09-19 04:35:52 字数 2044 浏览 5 评论 0原文

屏幕显示四个按钮。当按下按钮时,媒体播放器会播放声音。我遇到的问题是同时为所有按钮实现 setClickable 。

单击按钮后,我希望所有按钮都不可单击,直到媒体播放器完成播放与按钮单击相关的声音。然后我希望将所有按钮设置回可单击状态。

代码运行良好,直到我启用 setClickable 代码——在下面的代码示例中,buttonOne 的代码被禁用。测试手机锁定并告诉我应用程序已停止并重试。

不幸的是,如果没有 setClickable,用户可以在第一个选定的声音播放完毕之前按下任何按钮并听到任何声音。

感谢您的时间和帮助。

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;


public class hipsterdoofus  extends Activity
{
 private int asoundfilenumber;//integer id of sound file to be played

 public ImageButton buttonOne;
 public ImageButton buttonTwo;
 public ImageButton buttonThree;
 public ImageButton buttonFour;


 public void myClickHandler(View v) {



    switch (v.getId())
       {

        case R.id.buttonOne:
         asoundfilenumber=0x7f040000;
         break;

        case R.id.buttonTwo:
         asoundfilenumber=0x7f040001;
         break;

        case R.id.buttonThree:
         asoundfilenumber=0x7f040002;
         break;

        case R.id.buttonFour:
         asoundfilenumber=0x7f040003;
         break;   



        }//closes switch test



    freezeButtonsAndPlaySoundThenUnfreezeButtons();

  }//closes onClick


  public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
 {

  **//buttonOne.setClickable( false );//sets buttonOne to unclickable**

  MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
  mp.start();


  mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
   {

   @Override
   public void onCompletion(MediaPlayer mpalmost) 
    {
    mpalmost.release();
    }



   });

  **//buttonOne.setClickable( true ); //sets buttonOne to clickable**

 }


 public void onCreate(Bundle savedInstanceState) {
     super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

    }

The screen displays four buttons. When a button is pressed, a media player plays a sound. The problem I'm having is implementing setClickable for all buttons at the same time.

Once a button is clicked, I want all buttons to be unclickable until the media player is finished playing the sound associated with the button click. Then I want all buttons to be set back to clickable.

The code runs fine until I enable the setClickable code--the code for buttonOne is disabled in my code sample below. The test phone locks up and tells me the application has stopped and to try again.

Unfortunately, without setClickable, the user could press any button and hear any sound before the first selected sound is finished playing.

Thank you for your time and help.

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;


public class hipsterdoofus  extends Activity
{
 private int asoundfilenumber;//integer id of sound file to be played

 public ImageButton buttonOne;
 public ImageButton buttonTwo;
 public ImageButton buttonThree;
 public ImageButton buttonFour;


 public void myClickHandler(View v) {



    switch (v.getId())
       {

        case R.id.buttonOne:
         asoundfilenumber=0x7f040000;
         break;

        case R.id.buttonTwo:
         asoundfilenumber=0x7f040001;
         break;

        case R.id.buttonThree:
         asoundfilenumber=0x7f040002;
         break;

        case R.id.buttonFour:
         asoundfilenumber=0x7f040003;
         break;   



        }//closes switch test



    freezeButtonsAndPlaySoundThenUnfreezeButtons();

  }//closes onClick


  public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
 {

  **//buttonOne.setClickable( false );//sets buttonOne to unclickable**

  MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
  mp.start();


  mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
   {

   @Override
   public void onCompletion(MediaPlayer mpalmost) 
    {
    mpalmost.release();
    }



   });

  **//buttonOne.setClickable( true ); //sets buttonOne to clickable**

 }


 public void onCreate(Bundle savedInstanceState) {
     super.onCreate( savedInstanceState );
        setContentView( R.layout.main );

    }

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

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

发布评论

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

评论(2

婴鹅 2024-09-26 04:35:52

我认为您正在寻找的属性将是 setEnabled (用布尔值设置)

一些代码;

public void enableDisableButtons(Boolean state){
    buttonOne.setEnabled(state);
    buttonTwo.setEnabled(state);
    buttonThree.setEnabled(state);
    buttonFour.setEnabled(state);
}

public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
{
    enableDisableButtons(false); // disable buttons

    MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
    mp.start();


    mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
    {

        @Override
        public void onCompletion(MediaPlayer mpalmost) 
        {
            enableDisableButtons(true); // Re-enable buttons
            mpalmost.release();
        }
      });
}

i think the property you are looking for would be setEnabled (set with boolean)

some code;

public void enableDisableButtons(Boolean state){
    buttonOne.setEnabled(state);
    buttonTwo.setEnabled(state);
    buttonThree.setEnabled(state);
    buttonFour.setEnabled(state);
}

public void freezeButtonsAndPlaySoundThenUnfreezeButtons()
{
    enableDisableButtons(false); // disable buttons

    MediaPlayer mp = MediaPlayer.create(getBaseContext(), asoundfilenumber);
    mp.start();


    mp.setOnCompletionListener(new OnCompletionListener()//listens for player to finish then releases player
    {

        @Override
        public void onCompletion(MediaPlayer mpalmost) 
        {
            enableDisableButtons(true); // Re-enable buttons
            mpalmost.release();
        }
      });
}
不弃不离 2024-09-26 04:35:52

如果要禁用按钮,请使用 View 类的 setEnabled(false) 方法

If you want to disable a button use the method setEnabled(false) of the View class

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