簡體   English   中英

狀態不改變切換按鈕

[英]State not change of toggle button

當我單擊切換按鈕並向下滾動時,我具有帶切換按鈕的列表視圖,然后按鈕的狀態將更改為“關閉”狀態。

使用sharedpreference保存每個切換按鈕的狀態並從sharedpreference加載狀態

就像您可以在sharedpreference中定義切換狀態(具有唯一的ID)

此處在“自定義適配器”中定義了共享首選項值。

final ToggleButton tgl=(ToggleButton)row.findViewById(R.id.tglalertstatus);

            tgl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked)
                    {
                        SharedPreferences contact = context.getSharedPreferences(
                                "contact", 1);

                        editor = contact.edit();

                        editor.putInt("toggle"+tgl.getContentDescription().toString(), 1); // i set the content description for each toggle a unique string so it will work as a key for shared preference. 

                        editor.commit();
                    }
                    else
                    {
                        SharedPreferences contact = context.getSharedPreferences(
                                "contact", 1);

                        editor = contact.edit();

                        editor.putInt("toggle"+tgl.getContentDescription().toString(), 0);//i set the content description for each toggle a unique string so it will work as a key for shared preference.

                        editor.commit();
                    }

                }
            });

希望這會有所幫助。

暫無
暫無

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

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