如何为一个Button添加多个动作

时间:2016-08-27 07:58:01

标签: java android android-studio button countdown

我尝试在一个按钮中添加播放和暂停操作 这是我的代码我只是添加了播放动作,你能帮助我吗?

 fab_play_pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            timerHasStarted = true;
            countDownTimer.start();


            Resources res = getResources();
            Drawable drawable = res.getDrawable(R.drawable.custom_progressbar_drawable);
            final ProgressBar mProgress = (ProgressBar) rootView.findViewById(R.id.circularProgressbar);
            mProgress.setProgress(0);   // Main Progress
            mProgress.setSecondaryProgress(100); // Secondary Progress
            mProgress.setMax(100); // Maximum Progress
            mProgress.setProgressDrawable(drawable);
            mProgress.setRotation(0);

            ObjectAnimator animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
            animation.setDuration(startTime);
            animation.setInterpolator(new DecelerateInterpolator());
            animation.start();

        }
    });

2 个答案:

答案 0 :(得分:3)

你可以通过这个来实现它:

将其设置为默认为播放

    fab_play_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             String text = btnAdd.getText().toString();

               if(text.equalsIgnoreCase("Play")){
                  //your code
                   fab_play_pause.setText("pause")
               }
              else if(text.equalsIgnoreCase("pause"))
               {
                  //your code
                  fab_play_pause.setText("Play")
               }
            }
        });

对于Drawble:

 fab_play_pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                int resId = (Integer) view.getTag();

                   if(resId == R.drawable.play){
                      //your code
                      //change background for pause
                   }
                  else if(resId == R.drawable.pause)
                   {
                      //your code
                      //change background for play
                   }
                }
            });

答案 1 :(得分:0)

溶液

private static final String PLAY_ICON = "playIcon";
    private static final String PAUSE_ICON = "pauseImage";

    fab_play_pause.setImageResource(R.drawable.ic_play_arrow_white_24dp);
    fab_play_pause.setTag(PLAY_ICON);
    fab_play_pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String tag = (String) view.getTag();

            if (view.getTag() == PLAY_ICON){
                Toast.makeText(getContext(), "play",Toast.LENGTH_LONG).show();
                fab_play_pause.setTag(PAUSE_ICON);
            }else if (view.getTag() == PAUSE_ICON){
                Toast.makeText(getContext(), "pause",Toast.LENGTH_LONG).show();

            }
        }
    });