我有Button In片段,我已设置按钮的初始颜色为绿色,按钮点击后我将按钮颜色设置为红色,但是当我滑动并转到另一个片段并且在第一个片段出现后,按钮颜色设置初始颜色不是红色请帮助,关闭我的应用程序并重新启动应用程序后,颜色为初始颜色。
点击
这是我的Code For按钮@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();
}
答案 0 :(得分:0)
为此,您需要将按钮的状态保存在存储中。我建议你使用SharedPreferences
的简单解决方法
setButtonState(R.id.button1, "clicked")
将按钮状态存储在sharedPreference中。现在每当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();
}
现在在片段
的onResume()中写这个if(getButtonState(R.id.button1).equals("clicked")){
//apply clicked button color
} else {
//apply another color
}
答案 1 :(得分:0)
当您单击按钮时,在SharedPreferences中存储Button的颜色,每当您再次使用该相同的片段时,使用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
}
e.g。在片段中查看此内容。根据您的代码,当您单击该按钮时,您将缺少设置颜色状态。只是试图获得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;
}
}