Changing background color with Radio Buttons Android

女生的网名这么多〃 提交于 2019-12-23 16:28:34

问题


I am attempting to change the background of a tab of my application by selecting a Radio Button from a RadioGroup, however I am not sure how to go about this.

So far I have Favs.java:

import android.app.Activity;
import android.os.Bundle;

public class Favs extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.favs);
  }
}

which points to this .xml file -> favs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
              android:orientation="vertical" 
           xmlns:android="http://schemas.android.com/apk/res/android" 
              android:layout_height="fill_parent">
   <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What is your favorite color?" 
            android:padding="3dip"/>
   <RadioGroup android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:orientation="vertical">
       <RadioButton android:id="@+id/radio_red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Red" 
                    android:onClick="onClick" />
       <RadioButton android:id="@+id/radio_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yellow" />
    </RadioGroup>
</LinearLayout>

I understand that the android:onClick="onClick" needs to be there however I have no idea what to do after this.


回答1:


hi use below code for changing the background according to radio button selection

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
              android:orientation="vertical" 
              android:id="@+id/LinearLayout"
           xmlns:android="http://schemas.android.com/apk/res/android" 
              android:layout_height="fill_parent">
   <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="What is your favorite color?" 
            android:padding="3dip"/>
   <RadioGroup android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:id="@+id/Group1"
               android:orientation="vertical">
       <RadioButton android:id="@+id/radio_red"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Red" 
                    />
       <RadioButton android:id="@+id/radio_yellow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Yellow" />
    </RadioGroup>
</LinearLayout>

Activity

public class Change extends Activity {


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final LinearLayout ll=(LinearLayout) findViewById(R.id.LinearLayout);


        final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
        final RadioButton radio_yellow = (RadioButton) findViewById(R.id.radio_yellow);
        radio_red.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ll.setBackgroundColor(Color.RED);

            }
        });

 radio_yellow.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                ll.setBackgroundColor(Color.YELLOW);

            }
        });

    }
}


来源:https://stackoverflow.com/questions/4733079/changing-background-color-with-radio-buttons-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!