【问题标题】:Prevent Style Change of Button防止按钮样式更改
【发布时间】:2018-08-04 09:02:12
【问题描述】:

我最初使用此代码在onCreateView 设置按钮背景。

uc.setBackgroundResource(R.drawable.saat_button_none);

如果我最初设置按钮的背景或文本颜色,我想在使用 onClick 时防止样式更改

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bir:
            uc.setBackgroundResource(R.drawable.saat_button); //Should not work
            dort.setBackgroundResource(R.drawable.saat_button_sel);
            bes.setBackgroundResource(R.drawable.saat_button_sel);
 }
}

这可能吗?

编辑:我不想使用if statement,因为我有很多按钮,我只想锁定按钮的样式。

【问题讨论】:

  • 如果我最初设置了任何参数?这是什么意思?
  • 如果我最初设置按钮的背景或 textColor 我的意思是
  • 点击后比你想要的按钮还好吗?
  • 更改其他按钮的背景
  • 看看this

标签: java android android-fragments


【解决方案1】:

为此,只需通过扩展来创建自定义视图 View 并覆盖所有与背景相关的方法,然后将您的逻辑设置为如果背景已更改一次然后被覆盖方法应该throw exception saying that you can't change the style as it has been changed while setup the default look and feel

public class CustomButton extends Button {
  boolean backgroundChanged = true;
  public CustomButton(Context context) {
      this(context, null);
  }

  public CustomButton(Context context, @Nullable AttributeSet attrs) {
      this(context, attrs, 0);
  }

  public CustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
  }

  @Override
  public void setBackgroundResource(int resid) {
      if(backgroundChanged){
          throw new RuntimeException("you can't change the style as it has been changed while setup the default look and feel");
      }
      super.setBackgroundResource(resid);
  }
}

最后在布局文件中将<Button标签替换为<CustomButton

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2014-12-04
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多