2013-03-02 38 views
0

我是新來的android編程,我正在製作一個程序,當您按下三個單選按鈕之一時,它將更改textview項目的顏色。每個radiobutton對應於某種特定的顏色。代碼編譯並且不會給出任何錯誤,但它不會執行。 這是代碼Android RadioButtons控制textview的顏色

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> 
+1

您需要在onCreate中創建監聽器。你的ChangeState方法只是一個普通的方法,甚至都不會被調用。 – 2013-03-02 12:24:53

回答

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

      } 
     } 
    });