一键使用共享首选项在此处单击我的名片视图后,如何设置False?

已经在我的应用程序中实现了共享的首选项,其中单击Cardview后,应该不能再次单击它完成了维护Cardview的颜色和文本颜色的操作,但是很遗憾,您仍然可以单击Cardview,我想要也可以在单击共享首选项后将其禁用。 换句话说,一键单击后禁用这些单击并使用共享的首选项保存该状态,希望避免多次单击

这是我的代码

        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"));
    }
}
ppheadsman 回答:一键使用共享首选项在此处单击我的名片视图后,如何设置False?

您可以简单地在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,那不是不是要设置cardView吗?

if(cardviewsnal.equals ("false")){
    cardView.setEnabled (false); // was cardView.setEnabled(true) in your code
}
本文链接:https://www.f2er.com/2949419.html

大家都在问