Android RadioButtons控制文本视图的颜色

时间:2013-03-02 12:14:38

标签: android

我是Android编程的新手,我正在制作一个程序,当你按下三个单选按钮中的一个时,它会改变textview项目的颜色。每个radiobutton对应一些特定的颜色。代码编译并且不会给出任何错误,但它不会执行。 这是代码

package org.example.project2;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.EditText;
//import android.view.View.OnClickListener;
import android.graphics.Color;
public class MainActivity extends Activity {

    View mColorArea;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void ChangeState(View clickedbutton)
    {
        mColorArea=findViewById(R.id.color_place);
        RadioButton RB=(RadioButton)clickedbutton;
        CharSequence choice = RB.getText();
        //String Choice1=choice.toString(); this is right too
        String ChoiceString=getString(R.string.choice_string);
           String Choice1 =
                    String.format(ChoiceString, choice);
            if (Choice1=="Red")
            {
            mColorArea.setBackgroundColor(Color.RED);
            //mColorArea.setText("red");
            }
            else if (Choice1=="Yellow")
            {
            mColorArea.setBackgroundColor(Color.YELLOW);
            //mColorArea.setText("yellow");
            }
            else
            {
            mColorArea.setBackgroundColor(Color.BLUE);
            //mColorArea.setText("blue");
            }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这是xml部分

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="horizontal" 
         >
         <RadioButton
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/red_button" 
             android:onClick="ChangeState"
             />
         <RadioButton 
                       android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/blue_button" 
             android:onClick="ChangeState"
             />
         <RadioButton 
                       android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/yellow_button" 
             android:onClick="ChangeState"
             />
     </LinearLayout>
     <TextView
         android:id="@+id/color_place"
         android:layout_width="match_parent" 
         android:layout_height="match_parent"
         />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

你需要这样的东西

Red=(RadioButton)findViewById(R.id.radiobutton5);
        Blue=(RadioButton)findViewById(R.id.radiobutton6);
        Green=(RadioButton)findViewById(R.id.radiobutton7);


Red.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub


                if(isChecked)
                {
                    TextView t;
                    t.setTextColor(Color.RED);
                }
            }
        });


Green.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub


                if(isChecked)
                {
                    TextView t;
                    t.setTextColor(Color.Green);
                }
            }
        });

答案 1 :(得分:0)

您好请检查以下代码,根据选中的单选按钮设置背景颜色

 color.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub


            if(isChecked)
            {
               RelativeLayout yourlayout=(RelativeLayout)findViewById(R.id.your_layout_id);
               yourlayout=.setBackgroundColor(Color.RED);

            }
        }
    });