第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

保存按鈕的背景?

保存按鈕的背景?

桃花長(zhǎng)相依 2024-01-17 21:06:13
我有一個(gè)有背景的切換按鈕。我想在用戶關(guān)閉應(yīng)用程序或離開 MainActivity 時(shí)保存該背景狀態(tài)。我設(shè)法保存切換布爾值的狀態(tài),但不保存切換本身的圖像背景。有人有主意嗎?請(qǐng)參閱下面我的代碼以獲取更多信息。private ToggleButton mtoggle;public static final String SHARED_PREFS = "sharedPrefs";public static final String M_TOGGLE = "mtoggle";private boolean SwitchON_OFF;以下是我的保存、加載和更新方法。再次保存切換布爾值的狀態(tài)是沒有問(wèn)題的。只是不知道從哪里開始保存切換按鈕的背景狀態(tài)(一旦按下,背景就會(huì)從灰色變?yōu)辄S色,反之亦然)。需要保存它的每個(gè)狀態(tài)。public void saveData(){    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);    SharedPreferences.Editor editor = sharedPreferences.edit();    editor.putBoolean(M_TOGGLE,mtoggle.isChecked()); // saving toggle state no problem.    editor.apply();    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT ).show();}public  void loadData(){    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);    SwitchON_OFF = sharedPreferences.getBoolean(M_TOGGLE,false);}public void updateViews(){    mtoggle.setChecked(SwitchON_OFF);}
查看完整描述

1 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

你可以這樣做:-


根據(jù)您的要求,您可以使用 if 條件。


可繪制文件:- 兩種顏色:-


第一:-


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">


<stroke

    android:width="1dp"

    android:color="@color/sky_blue"

    android:drawable="@android:color/transparent" />


<solid android:color="@color/message_fragment_button" />


<corners

    android:bottomLeftRadius="@dimen/dp_10"

    android:bottomRightRadius="@dimen/dp_10"

    android:topLeftRadius="@dimen/dp_10"

    android:topRightRadius="@dimen/dp_10" />


</shape>

第二:-


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">


<stroke

    android:width="1dp"

    android:color="@color/sky_blue"

    android:drawable="@android:color/transparent" />


<solid android:color="@color/sky_blue" />


<corners

    android:bottomLeftRadius="@dimen/dp_10"

    android:bottomRightRadius="@dimen/dp_10"

    android:topLeftRadius="@dimen/dp_10"

    android:topRightRadius="@dimen/dp_10" />


</shape>

在 Java 中:-


if (MessageConfirmed.equals("Yes")) {

        btn_confirm.setText("Confirmed");

        btn_confirm.setBackground(ContextCompat.getDrawable(context, 

R.drawable.confirm_button_blue));

        btn_confirm.setEnabled(false);


    } else {

        btn_confirm.setText("Confirm");

        btn_confirm.setBackground(ContextCompat.getDrawable(context, 

R.drawable.confirm_button_green));

        btn_confirm.setEnabled(true);


}

但是,您需要在 XML 文件中定義最初的背景顏色。


查看完整回答
反對(duì) 回復(fù) 2024-01-17
  • 1 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)