如何通过按钮更改操作栏的颜色?

时间:2019-02-06 14:04:27

标签: java android

我想通过单击按钮保存颜色并将其设置为其他活动。

我使用代码对其进行了更改,但是此代码不会保存颜色。我应该在代码中添加什么?

mYellowColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportActionBar().setBackgroundDrawable(new 
                ColorDrawable(getResources().getColor(R.color.yello)));
            }
        });

1 个答案:

答案 0 :(得分:1)

在班级中定义共享首选项

private void Button_Click(object sender, EventArgs e)
{
  int count = 0;
  for (int row = 0; row < _dgv.Rows.Count; ++row)
  {
    if (_dgv.Rows[row].Cells[_comboBoxColumnNumberOrName].Value == "Yes")
    {
      count++;
    }
  }

  textBox1.Text = $"{count}";
}

在onCreate内添加

private SharedPreferences sharedPreferences;

然后在onClickListener内

sharedPreferences = getSharedPreferences("ShaPreferences", Context.MODE_PRIVATE);

在下一个活动中,再次定义“共享首选项”,并将以下代码添加到onCreate

        mYellowColor.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    String hexColor = "ADD YOUR HEX CODE HERE";
                    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(hexColor)));
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putString("toolbarColor",hexColor);
                    editor.commit();

                    }
                });
相关问题