亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

保存按鈕的背景?

保存按鈕的背景?

桃花長相依 2024-01-17 21:06:13
我有一個有背景的切換按鈕。我想在用戶關閉應用程序或離開 MainActivity 時保存該背景狀態。我設法保存切換布爾值的狀態,但不保存切換本身的圖像背景。有人有主意嗎?請參閱下面我的代碼以獲取更多信息。private ToggleButton mtoggle;public static final String SHARED_PREFS = "sharedPrefs";public static final String M_TOGGLE = "mtoggle";private boolean SwitchON_OFF;以下是我的保存、加載和更新方法。再次保存切換布爾值的狀態是沒有問題的。只是不知道從哪里開始保存切換按鈕的背景狀態(一旦按下,背景就會從灰色變為黃色,反之亦然)。需要保存它的每個狀態。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貢獻1828條經驗 獲得超3個贊

你可以這樣做:-


根據您的要求,您可以使用 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 文件中定義最初的背景顏色。


查看完整回答
反對 回復 2024-01-17
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號