更改背景颜色单选按钮的Andr​​oid [英] Changing background color with Radio Buttons Android

查看:100
本文介绍了更改背景颜色单选按钮的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从RadioGroup中选择一个单选按钮,以改变我的应用程序的选项卡的背景下,但我不知道如何去这一点。

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.

到目前为止,我有
Favs.java:

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

一个指向该.xml文件 - > favs.xml

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>

据我了解,安卓的onClick =的onClick需要在那里,但是我不知道在这之后该怎么办

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

推荐答案

喜根据单选按钮选择使用下面code为改变背景

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>

活动

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

            }
        });

    }
}

这篇关于更改背景颜色单选按钮的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆