【问题标题】:Set Button Background Color In fragment after button click单击按钮后在片段中设置按钮背景颜色
【发布时间】:2016-09-19 15:36:42
【问题描述】:

我在片段中有按钮,并且我已将按钮的初始颜色设置为绿色,单击按钮后我将按钮颜色设置为红色,但是当我滑动并转到另一个片段并在第一个片段之后按钮颜色设置初始颜色不是红色请帮忙,在关闭我的应用程序并重新启动应用程序后,颜色是初始颜色。

这是我点击按钮的代码

@Override
public void onClick(View v) {
    switch (v.getId()) {

        case R.id.table1:

                btnColor = (ColorDrawable) table1.getBackground();
                colorId = btnColor.getColor();
                if (colorId == getResources().getColor(R.color.colorAcce)) {
                    String value = table1.getText().toString();
                    Intent intent = new Intent(getActivity(), TakeOrderActivity.class);
                    intent.putExtra("myString", value);
                    startActivity(intent);
                    table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));

            }else
            {
                ((MainActivity)getActivity()).navigateFragment(1);
            }

            break;
        case R.id.table2:
            btnColor = (ColorDrawable) table2.getBackground();
            colorId = btnColor.getColor();
            if (colorId == getResources().getColor(R.color.colorAcce)){
            String value1 = table2.getText().toString();
            Intent intent1 = new Intent(getActivity(), TakeOrderActivity.class);
            intent1.putExtra("myString", value1);
            startActivity(intent1);
            table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));
            }else
            {
                ((MainActivity)getActivity()).navigateFragment(1);
            }
            break;
}

这是修改后的代码

public String getButtonState(int id) {
   SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
   return pref.getString("btn"+id, "not_clicked");
}

public void setButtonState(int id,String state) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.Editor editor = pref.edit();
    editor.putString("btn"+id, state);
    editor.commit();
}
@Override
public void onResume() {
    if(getButtonState(R.id.table1).equals("clicked")){
        table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce));
    } else {
        table1.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));

    }
    if(getButtonState(R.id.table2).equals("clicked")){
        table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce));
    } else {
        table2.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));
    }
    if(getButtonState(R.id.table9).equals("clicked")){
        table9.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));
    } else {
        table9.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce));
    }
    if(getButtonState(R.id.table11).equals("clicked")){
        table11.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAccent));
    } else {
        table11.setBackgroundColor(getActivity().getResources().getColor(R.color.colorAcce));
    }
    super.onResume();
}

【问题讨论】:

  • 您必须将按钮的颜色保存在内存中的某个位置,否则 Android 不知道您是否更改了按钮颜色。
  • 我可以使用共享偏好来存储颜色吗?
  • 使用saveinstance保存fragmnet状态
  • @Dipak 是的,SharedPreferences 是一个很好的方法。
  • @Dipak 检查我的答案。

标签: android fragment android-tablayout


【解决方案1】:

为此,您需要将按钮的状态保存在存储中。我建议你使用 SharedPreferences

简单的解决方法

  1. 使用 setButtonState(R.id.button1, "clicked") 方法将按钮状态存储在 sharedPreference 中。
  2. 现在每次onResume() 调用的片段加载值来自 SharedPreferences。

    public String getButtonState(int id) {
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        return pref.getString("btn"+id, "not_clicked");
    }
    
    public void setButtonState(int id,String state) {
        SharedPreferences pref; = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        Editor editor = pref.edit();
        editor.putString("btn"+id, state);
        editor.commit();
    }
    
  3. 现在写在片段的 onResume() 中

    if(getButtonState(R.id.button1).equals("clicked")){
        //apply clicked button color
    } else {
        //apply another color
    }
    

【讨论】:

  • 你说的有两种状态。当您第一次单击按钮时,您需要致电setButtonState("clicked")。现在,每次当您 onResume 时,您都需要检查该按钮是否已被单击。如果它点击了,那么你需要应用红色否则绿色。
  • 您需要将 button_ID 作为字符串传递给密钥。所以每个按钮都有其独特的 sharedPreference。我正在编辑问题
  • 我已经更新了我的代码,但是在点击按钮之前颜色被设置为红色。
  • 您将错误的颜色传递给表 9 和表 11 按钮
  • 是的,我也试过但是当改变它在按钮点击之前设置为红色
【解决方案2】:

当您单击按钮时,将 Button 的颜色存储在 SharedPreferences 中,并且每当您再次访问同一个片段时,使用 SharedPreferences 存储的颜色状态将 Button 的颜色从初始(绿色)更改为红色。

点击按钮:

SharedPreferences preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
Editor editor = preferences.edit();
editor = editor.putString("BUTTON_COLOR", "Red").commit();

检查 UI 可见性:

if(preferences.getString("BUTTON_COLOR", "").equalsIgnorCase("Red")){
//Set button color to Red
}else{
//Set button color to green
}

例如在你的片段中检查这个。根据您的代码,您在单击该按钮时缺少设置颜色状态。仅尝试在 onResume 中获取颜色状态,您将如何直接设置任何内容。 勾选以下一项即可完成。

//Variables
private SharedPreferences preferences;
private Editor editor;
private Button btnClickMe;

//In onCreate of fragment
preferences = getSharedPreferences("AppName", Activity.MODE_PRIVATE);
editor = preferences.edit();

//In onCreateView of fragment
btnClickMe = (Button) findViewById(R.id.btnClickMe);
btnClickMe.setOnClickListener(this);

@Override
    protected void onResume() {
        super.onResume();
        if(preferences.getString("BUTTON_COLOR", "Green").equalsIgnoreCase("Green")){
            btnClickMe.setBackgroundColor(getResources().getColor(R.color.green));
        }else{
            btnClickMe.setBackgroundColor(getResources().getColor(R.color.red));
        }
    }

@Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnClickMe:
            if((((ColorDrawable)btnClickMe.getBackground()).getColor()) == getResources().getColor(R.color.red)){
                //navigate to activity
                editor.putString("BUTTON_COLOR", "Green").commit();
                btnClickMe.setBackgroundColor( getResources().getColor(R.color.green));
            }else{
                //navigate to fragment
                editor.putString("BUTTON_COLOR", "Red").commit();
                btnClickMe.setBackgroundColor( getResources().getColor(R.color.red));
            }
            break;
        default:
            break;
        }
    }

【讨论】:

  • 请任何人给我正确的解决方案,以便在片段刷新后更改按钮的颜色
  • 但是我有很多按钮,如果我使用上面的代码设置颜色,它会在点击之前显示红色。
  • 这里我使用 BUTTON_COLOR 因为我只有一个按钮。对你来说,像这样对每个按钮使用 BUTTON_1_COLOR、BUTTON_2_COLOR、BUTTON_3_COLOR。
猜你喜欢
  • 2015-01-26
  • 2017-01-26
  • 1970-01-01
  • 2021-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2021-10-29
相关资源
最近更新 更多