简体   繁体   中英

how can i add multiple actions for one Button

i try to add play and pause action in one button this is my code i just add play action, can you help me, please?

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

        }
    });

You can achieve it by this:

set it default to Play

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

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

solution

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

            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM