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

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

選擇/取消選擇時(shí)如何更改單選按鈕的背景顏色/形狀

選擇/取消選擇時(shí)如何更改單選按鈕的背景顏色/形狀

富國滬深 2022-05-25 10:42:59
我正在以編程方式將單選按鈕添加到 for 循環(huán)中的 RadioGroup 中。我希望我的按鈕在未選中時(shí)具有某種背景顏色,而在選中時(shí)具有另一種背景顏色(一次只能選擇一個(gè))。我的問題是我也希望這個(gè)背景是一個(gè)圓角矩形。我讓它部分工作,但每當(dāng)我選擇一個(gè)答案時(shí),它會將該按鈕的背景設(shè)置為白色,但不會重置任何其他按鈕。我知道我需要某種類型的選擇器對象來添加無線電組所期望的切換行為。我能夠找到一些例子,但他們只展示了如何將純色設(shè)置為背景。我需要使用這種圓角形狀。所以,我的麻煩是弄清楚如何將兩者融合在一起。我可能完全錯(cuò)了。如果有更好的方法來實(shí)現(xiàn)我想要的,我寧愿使用它而不是挽救我已經(jīng)嘗試過的東西。我可以通過創(chuàng)建兩個(gè) XML 文件來接近,每個(gè)文件都有一個(gè)形狀對象和自己的背景顏色。如果選擇了一個(gè)單選按鈕,它的樣式將設(shè)置為 radio_button_checked" 樣式。但是,我知道我正在接近這個(gè)錯(cuò)誤,因?yàn)榱硪粋€(gè)選中的按鈕沒有關(guān)閉。我基本上可以單擊所有按鈕并讓它們都變成白色背景。我知道有更好的方法可以做到這一點(diǎn),我只是不知道它是什么這是我添加按鈕的循環(huán),以及 radiogroup rg 的更改偵聽器(rg 是我的 RadioGroup,答案只是字符串的 HashMap。它們不會影響與此問題相關(guān)的任何內(nèi)容。buttonTintList 行就在那里改變圓圈顏色)for(int i = 0; i < answers.size(); i++)    {        RadioButton rb = new RadioButton(context);        rb.setTextColor(ContextCompat.getColor(context, R.color.colorPrimaryDark));        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            rb.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor( context, R.color.colorAccent)));        }        rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_unchecked));        rb.setText(answers.get(i));        rb.setWidth(800);        rb.setHeight(150);        rb.setTextSize(18);        rb.setLayoutParams(params);        rg.addView(rb);    }    /*  Store the answer */    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(RadioGroup group, int checkedId) {            RadioButton rb= (RadioButton)group.findViewById(checkedId);            answer = rb.getText().toString();            rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_checked));        }    });
查看完整描述

2 回答

?
慕哥6287543

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

試試這個(gè)


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

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/holo_blue_light"

    android:orientation="vertical">


    <RadioGroup

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">


        <RadioButton

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:background="@drawable/radio_background"

            android:padding="10dp"

            android:text="Yes" />


        <RadioButton

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:background="@drawable/radio_background"

            android:padding="10dp"

            android:text="No" />


    </RadioGroup>


</LinearLayout>

@drawable/radio_background


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

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

    <item android:drawable="@android:color/white" android:state_checked="true" />

    <item android:drawable="@color/colorAccent" android:state_checked="false" />

</selector>

輸出

http://img1.sycdn.imooc.com//628d97db0001353707170284.jpg

更新

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

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

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/holo_blue_light"

    android:orientation="vertical">


    <RadioGroup

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">


        <RadioButton

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:background="@drawable/test"

            android:padding="10dp"

            android:text="Yes" />


        <RadioButton

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp"

            android:background="@drawable/test"

            android:padding="10dp"

            android:text="No" />


    </RadioGroup>


</LinearLayout>

@drawable/測試


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

<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">


    <item android:drawable="@drawable/radio_selected" android:state_checked="true" />

    <item android:drawable="@drawable/radio_normal" />


</selector>

@drawable/radio_selected


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

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

    android:shape="rectangle">

    <corners android:radius="20dp" />

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

    <padding

        android:bottom="5dp"

        android:left="0dp"

        android:right="0dp"

        android:top="5dp" />



</shape>

@drawable/radio_normal


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

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

    android:shape="rectangle">

    <corners android:radius="20dp" />

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

    <padding

        android:bottom="5dp"

        android:left="0dp"

        android:right="0dp"

        android:top="5dp" />


</shape>

輸出

http://img1.sycdn.imooc.com//628d97f00001a96007150315.jpg

查看完整回答
反對 回復(fù) 2022-05-25
?
拉莫斯之舞

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

我建議你有 3 個(gè)可繪制的 xml

  1. radio_checked.xml(你要檢查的效果)

  2. radio_unchecked.xml(你想取消選中的效果)

  3. radio_custom_drawable.xml

    在此 xml 中,您必須執(zhí)行以下操作:

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

<item android:state_checked="true" 

 android:drawable="@drawable/radio_checked" />

<item android:state_checked="false" 

 android:drawable="@drawable/radio_unchecked" />

</selector>

然后在你的代碼中改變這個(gè)


rb.setBackground(context.getResources().getDrawable(R.drawable.radiobutton_style_checked));


rb.setBackground(context.getResources().getDrawable(R.drawable.radio_custom_drawable));



查看完整回答
反對 回復(fù) 2022-05-25
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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