Java 如何使用共享首选项单击此处的卡视图后将Enabled false设置为false
在我的应用程序中实现了共享首选项,在单击cardview后,不应再次单击它。已完成了保持cardview颜色和文本颜色的部分工作,但不幸的是,您仍然可以单击cardview,我希望在单击后使用共享首选项将其禁用。 换句话说,单击一次后,禁用单击并使用共享首选项保存该状态,要避免多次单击吗 这是我的代码Java 如何使用共享首选项单击此处的卡视图后将Enabled false设置为false,java,android,sharedpreferences,android-cardview,Java,Android,Sharedpreferences,Android Cardview,在我的应用程序中实现了共享首选项,在单击cardview后,不应再次单击它。已完成了保持cardview颜色和文本颜色的部分工作,但不幸的是,您仍然可以单击cardview,我希望在单击后使用共享首选项将其禁用。 换句话说,单击一次后,禁用单击并使用共享首选项保存该状态,要避免多次单击吗 这是我的代码 mPreferences = PreferenceManager.getDefaultSharedPreferences (getContext ()); mEdi
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"));
}
}
您只需在onClick Listener中编辑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
}