簡體   English   中英

使用共享首選項在此處單擊我的卡片視圖后,如何將 Enabled 設為 false

[英]how can i set Enabled false after one click on my card view here using shared preferences

在我的應用程序中實現了共享首選項,在點擊 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"));
    }
}

您可以簡單地編輯 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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM