【问题标题】:how can i add multiple actions for one Button如何为一个按钮添加多个操作
【发布时间】:2017-01-03 20:48:02
【问题描述】:

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

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

        }
    });

【问题讨论】:

  • 保持布尔检查您的状态并使用简单的 if else 执行有关状态的操作

标签: java android android-studio button countdown


【解决方案1】:

您可以通过以下方式实现它:

设置它默认为播放

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

对于绘图:

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

【讨论】:

  • == --> equals
  • 我宁愿看到按钮对布尔状态变量做出反应,也不愿看到已经对状态做出反应的东西(文本或 UI 中的可绘制)...
【解决方案2】:

解决方案

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

            }
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多