【问题标题】:how to get current colour of FloatingActionButton如何获取 FloatingActionButton 的当前颜色
【发布时间】:2021-01-24 13:05:45
【问题描述】:

我的应用中有 FloatingActionButton: https://i.stack.imgur.com/iOj2C.jpg 女巫的配置与上面链接中的图片一致。

在我的应用中,用户可以通过单击此按钮来更改此 FloatingActionButton 的颜色。

 fabUP = findViewById(R.id.fab_dwn);
    fabUP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Saving mode", Snackbar.LENGTH_LONG).setAction("SAVE", null).show();
            fabDOWN.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.FLOAT_dark_GREEN)));
            fabUP.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.FLOAT_bright_RED)));
            SAVE=true;
        }
    });

我想在 onSaveInstanceState 方法上保存这个按钮的颜色,并在 onRestoreInstanceState 方法上设置。

   @Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    //here I would like to save the colour of my FloatingActionButton
}

@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if(savedInstanceState!=null)
    {
       //and here I would like to set the colour of my FloatingActionButton
    }
}

如何做到这一点?

【问题讨论】:

    标签: android colors floating-action-button material-components-android


    【解决方案1】:

    你可以使用:

    fab.getBackgroundTintList()
    

    【讨论】:

      【解决方案2】:

      对我来说很好用:

      @Override
      protected void onSaveInstanceState(@NonNull Bundle outState) {
          super.onSaveInstanceState(outState);
          
          if(fabUP!=null) 
          { 
              outState.putString("fabUP", fabUP.getSupportBackgroundTintList().getDefaultColor()+""); 
          }
      }
      

      @Override
      protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          if(savedInstanceState!=null)
          {
              fabUP.setBackgroundTintList(ColorStateList.valueOf(Integer.parseInt(savedInstanceState.getString("fabUP"))));
          }
      }
      

      【讨论】:

        【解决方案3】:

        要在 onSaveInstanceState 方法上保存颜色,您可以使用类似

        protected void onSaveInstanceState(@NonNull Bundle outState) {
            super.onSaveInstanceState(outState);
            //here I would like to save the colour of my FloatingActionButton
            outstate.putParcelable("fab_up", fabUP.getBackgroundTintList())
            outstate.putParcelable("fab_down", fabDOWN.getBackgroundTintList())
        }
        

        恢复你可以使用的颜色

        protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            if(savedInstanceState!=null)
            {
               //and here I would like to set the colour of my FloatingActionButton
                fabDOWN.setBackgroundTintList(savedInstanceState.getParcelable("fab_down"));
                fabUP.setBackgroundTintList(savedInstanceState.getParcelable("fab_up"));
        
            }
        }
        
        

        【讨论】:

        • 毗湿奴你的提议是最优化的。谢谢。
        猜你喜欢
        • 2020-04-21
        • 2021-06-05
        • 2015-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多