Изменение цвета фона с помощью радиопроизводств Android

StackOverflow https://stackoverflow.com/questions/4733079

Вопрос

Я пытаюсь изменить фон вкладки моего приложения, выбрав радионологическую кнопку из радиограммы, однако я не уверен, как это сделать.

Пока что у меня есть любимые.

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);
  }
}

который указывает на этот файл .xml -> 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>

Я понимаю, что Android: Onclick = "onclick" должен быть там, однако я понятия не имею, что делать после этого.

Это было полезно?

Решение

HI Используйте ниже код для изменения фона в соответствии с выбором радиопроизводительной кнопки

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>

Мероприятия

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);

            }
        });

    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top