【问题标题】:how to remove stroke after adding it to a button将笔划添加到按钮后如何删除笔划
【发布时间】:2018-02-28 11:37:02
【问题描述】:

我试图在按下按钮时为其添加边框(笔触),然后在按下另一个按钮时删除笔触。问题是按钮保持按钮+笔画的大小,而不是回到原始大小(没有笔画)

我尝试的代码如下所示:

front.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.getPaint().setColor(Color.RED);
            shapeDrawable.getPaint().setStrokeWidth(30f);
            shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
            front.setBackgroundDrawable(shapeDrawable);

            return false;
        }
    });

    back.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.getPaint().setColor(Color.GRAY);
            shapeDrawable.getPaint().setStrokeWidth(0f);
            shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
            front.setBackgroundDrawable(shapeDrawable);

            return false;
        }
    });

【问题讨论】:

    标签: android android-button


    【解决方案1】:

    我发现解决此问题的最佳方法是将背景颜色设置为所需的颜色,而不是使可绘制对象为空。对于上面的代码示例,我放置了以下代码,以便能够将按钮恢复为灰色:

    front.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
    
            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.getPaint().setColor(Color.RED);
            shapeDrawable.getPaint().setStrokeWidth(30f);
            shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
            front.setBackgroundDrawable(shapeDrawable);
            back.setBackgroundColor(Color.GRAY);
    
            return false;
        }
    });
    
    back.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
    
            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.getPaint().setColor(Color.GRAY);
            shapeDrawable.getPaint().setStrokeWidth(0f);
            shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
            back.setBackgroundDrawable(shapeDrawable);
            front.setBackgroundColor(Color.GRAY);
    
            return false;
        }
    });
    

    【讨论】:

      【解决方案2】:

      尝试将另一个按钮的背景可绘制对象设置为空。另外,请注意您在第二种方法中遇到了错误 - 您使用的是 front.setBackgroundDrawable(shapeDrawable); 而不是 back.setBackgroundDrawable(shapeDrawable);。供您使用的工作代码:

      front.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View view, MotionEvent event) {
      
              ShapeDrawable shapeDrawable = new ShapeDrawable();
              shapeDrawable.getPaint().setColor(Color.RED);
              shapeDrawable.getPaint().setStrokeWidth(30f);
              shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
              front.setBackgroundDrawable(shapeDrawable);
              back.setBackgroundDrawable(null);
      
              return false;
          }
      });
      
      back.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View view, MotionEvent event) {
      
              ShapeDrawable shapeDrawable = new ShapeDrawable();
              shapeDrawable.getPaint().setColor(Color.GRAY);
              shapeDrawable.getPaint().setStrokeWidth(0f);
              shapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
              back.setBackgroundDrawable(shapeDrawable);
              front.setBackgroundDrawable(null);
      
              return false;
          }
      });
      

      【讨论】:

      • 将背景可绘制对象设置为 null 完全摆脱了按钮,只留下按钮上的文本。有没有办法将drawable设置为null但保留按钮的填充颜色?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多