gpt4 book ai didi

java - 保存 Button 的颜色并将其与 SharedPreferences 一起使用

转载 作者:行者123 更新时间:2023-12-01 11:03:34 25 4
gpt4 key购买 nike

在android中,我有一个三个按钮yes no Maybe,这里按钮在用户点击时颜色会改变,并且根据用户点击的按钮存储为1 0 2。在这里,如果我关闭或只是执行某些操作并返回该按钮,它的颜色不会改变。看起来很正常。当我们返回应用程序时如何保持该颜色

这是是,否,也许按钮的代码

yesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// highlight the button when clicked
yesBtn.setBackgroundColor(Color.YELLOW);
noBtn.setBackgroundColor(Color.BLUE);
maybeBtn.setBackgroundColor(Color.BLUE);
responseLayout.setVisibility(View.GONE);
//If user clicks yes button in invitation response layout,response would be stored as 1 for event user
final int response = 1;
final long eventId = eventMOs.get(position).getEventId();
userMO.setIsAttending(response);

new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

}

}.execute(null, null, null);

}

});
noBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesBtn.setBackgroundColor(Color.BLUE);
noBtn.setBackgroundColor(Color.YELLOW);
maybeBtn.setBackgroundColor(Color.BLUE);
responseLayout.setVisibility(View.GONE);
//If user clicks no button in invitation response layout,response would be stored as 0 for event user
final int response = 0;
final long eventId = eventMOs.get(position).getEventId();
userMO.setIsAttending(response);

new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

}

}.execute(null, null, null);
}
});
maybeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yesBtn.setBackgroundColor(Color.BLUE);
noBtn.setBackgroundColor(Color.BLUE);
maybeBtn.setBackgroundColor(Color.YELLOW);
responseLayout.setVisibility(View.GONE);
//If user clicks maybe button in invitation response layout,response would be stored as for event user
final int response = 2;
userMO.setIsAttending(response);
final long eventId = eventMOs.get(position).getEventId();

new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateEventUserRelationShipMapping(userMO, eventId);

}

}.execute(null, null, null);
}
});

单击事件时会显示此是或否按钮event 是一个 ListView 。单击该 ListView 中的任何事件时,此 yes no Maybe 响应布局会显示

这里如何使用共享首选项,如果我在"is"按钮下使用共享首选项,如下所示

SharedPreferences prefs = getSharedPreferences("mypref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();

它显示错误无法解析方法 getSharedPreferences

最佳答案

无论您是否单击该按钮,只需保存在共享首选项中即可。并将此代码放入 onClick 按钮中

SharedPreference sharedpreferences prefs =   
getSharedPreferences("UI_Elements", Context.MODE_PRIVATE);

Editor editor = prefs.edit;
editor.putString("buttonClicked","true");
editor.commit();

现在您需要在 onCreate() 中检查您的按钮是否被单击。检查值为

if(prefs.getString("buttonClicked","false").equals("true")){

//单击按钮后您想要提供的任何颜色都在此处给出。

yesBtn.setBackgroundColor(Color.BLUE);

}

关于java - 保存 Button 的颜色并将其与 SharedPreferences 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33150231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com