【问题标题】:How to make button flashing after pressed?按下后如何使按钮闪烁?
【发布时间】:2014-09-20 23:47:44
【问题描述】:

我想让 Mediaplayer 按钮在按下后开始闪烁,并停止闪烁,直到我按下下一个 Mediaplayer 按钮,该按钮也开始闪烁。我现在有按钮在按下时闪烁,但现在的问题是当我按下下一个按钮时如何让它们停止闪烁。现在,当我按下下一个按钮时,按钮会继续闪烁。

这是我现在使用的代码:

        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);

        mAnimation = new AlphaAnimation(1, 0);
        mAnimation.setDuration(500);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.setRepeatCount(Animation.INFINITE);
        mAnimation.setRepeatMode(Animation.REVERSE);

        mButton1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                mButton2.clearAnimation();
                mButton1.startAnimation(mAnimation);

                {
                    mp.release();
                    mp = MediaPlayer.create(Activity2.this, R.raw.audio_1);
                    mp.setLooping(true);
                    mp.start();


                }
                }
        });

        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mButton1.clearAnimation();
                mButton2.startAnimation(mAnimation);

                {
                mp.release();
                mp = MediaPlayer.create(Activity2.this, R.raw.audio_2);
                mp.setLooping(true);
                mp.start();


            }
            }
        });

【问题讨论】:

  • 你能解释一下吗?你想从一组按钮中只闪烁一个按钮吗?当按下一个按钮时,它开始闪烁,之前闪烁的按钮应该停止?
  • 正是我想要的!
  • 我有带有 12 个不同按钮/音频的 Mediaplayer,我希望这些按钮一次闪烁一个,具体取决于我按下的按钮。

标签: java android


【解决方案1】:

试试这个方法

mButton1 = (Button)findViewById(R.id.button1);
mButton2 = (Button)findViewById(R.id.button2);

mAnimation = new AlphaAnimation(1, 0);
mAnimation.setDuration(500);
mAnimation.setInterpolator(new LinearInterpolator());
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.REVERSE);

mBbutton1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mButton2.clearAnimation();
        mButton1.startAnimation(mAnimation);
    }
});

mBbutton2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mButton1.clearAnimation();
        mButton2.startAnimation(mAnimation);
    }
});

请注意,在 button1 点击监听器上,您应该在 button1 中启动动画并清除 button2 的动画,反之亦然

如果您有很多按钮,请尝试将它们存储在数组中并执行逻辑

mButtons = new ArrayList<Button>();

mClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //stop flashing all buttons
        for(Button button : mButtons) {
            button.clearAnimation();
        }
        //start animation on this button
        v.startAnimation(mAnimation);
    }
});

Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(mClickListener);
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(mClickListener);
//get as many as buttons

mButtons.add(button1);
mButtons.add(button2);
//add all the buttons to array

【讨论】:

  • 感谢您的帮助,如果您现在用 3 个不同的按钮/音频查看我的代码。我应该如何开始构建它。我试过了,但我得到了很多错误..
  • 嗨,我终于让按钮正确闪烁,但现在我听不到音频文件了。你能告诉我我做错了什么吗?检查我编辑的代码?
  • 在按钮单击侦听器中未调用运行音频播放的代码。在每个点击监听器的 startAnimation() 方法之后添加代码以运行音频。这是您的 btn1() 和 btn2() 方法中的一段代码
  • 是的!现在它的工作!非常感谢.. 编辑代码现在如何.. :)
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 2020-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多