java - 如何使用共享首选项在此处单击我的卡片视图后将启用设置为 false
问题描述
在我的应用程序中实现了共享首选项,在单击卡片视图后,它应该无法再次单击已完成维护卡片视图颜色和文本颜色的部分,但不幸的是您仍然可以单击卡片视图,我也想要单击后使用共享首选项禁用。 换句话说,单击后禁用单击并使用共享首选项保存该状态,希望避免多次单击
这是我的代码
mPreferences = PreferenceManager.getDefaultSharedPreferences (getContext ());
mEditor = mPreferences.edit ();
checkSharedPreferences ();
//save the cardview preference
if(cardView.isEnabled ())
{
//set a cardview state
mEditor.putString (getString (R.string.cardviewsnal),"true");
//disabling the button after one click
mEditor.commit ();
//save the color of the cardview
mEditor.putString (getString (R.string.colorCardview),"#2b43f");
mEditor.commit ();
//save the textcolor pf the cardview
mEditor.putString (getString (R.string.textcolorsnal),"#ffffff");
mEditor.commit ();
} else
{
//set a cardview state
mEditor.putString (getString (R.string.cardviewsnal),"false");
//disabling the button after one click
cardView.setEnabled (false);
mEditor.commit ();
//save the color of the cardview
mEditor.putString (getString (R.string.colorCardview),"#ffffff");
mEditor.commit ();
//save the textcolor pf the cardview
mEditor.putString (getString (R.string.textcolorsnal),"#2b434f");
mEditor.commit ();
}
//setting the click listener to send the request to specific section api anf return response
cardView.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
if(cardView.getCardBackgroundColor ().getDefaultColor () ==-1 || textView.getTextColors ().getDefaultColor () ==-1)
{
//change background color
cardView.setCardBackgroundColor (Color.parseColor ("#2b434f"));
textView.setTextColor (Color.parseColor ("#ffffff"));
Toast.makeText (getActivity (),"Request Sent to SNAL",Toast.LENGTH_LONG).show ();
cardView.setEnabled (false);
}
else {
cardView.setCardBackgroundColor (Color.parseColor ("#ffffff"));
textView.setTextColor (Color.parseColor ("#2b434f"));
}
}
});
/**
* *Check the shared preferences and set them accordingly
*/
private void checkSharedPreferences()
{
String cardviewsnal = mPreferences.getString (getString (R.string.cardviewsnal),"false");
String colorCardview = mPreferences.getString (getString (R.string.colorCardview),"#ffffff");
String textcolorsnal = mPreferences.getString (getString (R.string.textcolorsnal),"#2b434f");
if(cardviewsnal.equals ("false")){
cardView.setEnabled (true);
}
else
{
cardView.setEnabled (true);
}
if(colorCardview.equals ("#ffffff"))
{
cardView.setCardBackgroundColor (Color.parseColor ("#ffffff"));
}
else
{
cardView.setCardBackgroundColor (Color.parseColor ("#2b434f"));
}
if(textcolorsnal.equals ("#2b434f"))
{
textView.setTextColor (Color.parseColor ("#2b434f"));
}
else {
textView.setTextColor (Color.parseColor ("#ffffff"));
}
}
解决方案
您可以简单地在 onClickListener 中编辑 onClick 方法:
@Override
public void onClick(View v) {
if(cardView.isEnabled()) {
cardView.setEnabled(false);
// do other things like writing to SharedPreferences and changing bg colors.
} else {
// set background colors
cardView.setCardBackgroundColor (Color.parseColor ("#ffffff"));
textView.setTextColor (Color.parseColor ("#2b434f"));
}
}
我认为那会奏效。另外,如果在您的 checkPreferences 方法中从内存中读取了 cardView,您的意思是不是要禁用它?
if(cardviewsnal.equals ("false")){
cardView.setEnabled (false); // was cardView.setEnabled(true) in your code
}
推荐阅读
- android - Android ConnectionService 来电
- ios - 检测应用何时滑过 iPad 上正在运行的应用
- ios - 在 Xcode 10 中使用 MapboxNavigation iOS
- google-apps-script - 如果前 10 列重复,则删除 Google 表格行的 Google 脚本
- android-fragments - 如何用动态内容约束 ScrollView?
- python - 从 pandas 或 dask 中的数据库表中读取大数据
- kubernetes - 每个功能分支部署的新 K8s 命名空间是一个好习惯吗?
- python - PANDAS:将 2 行 df 转换为单行多级列 df
- python - BeautifulSoup 和请求模块 NoneType 错误
- node.js - 如何将 Google oAuth 与 Node.js 后端和 Angular 前端一起使用?