2016-06-16 5 views
0

アクティビティのRadioButtonをクリックすると、アクティビティの背景色が変化するアンドロイドアプリケーションが作成されました。活動の背景色がに変わりますのRadioButtonのいずれかのクリックで enter image description hereAndroidのレイアウトの背景を動的に変更すると正しく動作しません

だから我々が持っている3つのRadioButton:アプリを開くと :

私は、画像の助けを借りて、私の質問を起草していますそれぞれの色。

=>最初にラジオボタンをクリックすると、背景色が正しく反映されます。例えば:

enter image description here

グリーン上で緑に背景色の変更をクリックした後。

=>しかし、その後、ボタンをクリックするたびに背景色が即座に反映されません。 例:

enter image description here

私はブルーをクリックしますが、背景色はまだ緑です。

=>他のラジオボタンをクリックすると、以前にクリックされたラジオボタンの背景色がアクティビティに反映されます。 例えば:私は赤をクリックした後

enter image description here

、その後、私は青と背景色を得ました。

この問題を解決するにはどうすればよいですか。私はラジオボタンがチェックされた直後に背景色を変更したい。

MainActivity.javaファイルは次のとおりです。

package com.aupadhyay.assignment2; 

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.CompoundButton; 
import android.widget.LinearLayout; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { 

    RadioGroup radioGroup; 
    RadioButton redRadioButton, greenRadioButton, blueRadioButton; 
    LinearLayout linearLayout; 

    public void initLinearLayout() 
    { 
     linearLayout = (LinearLayout) findViewById(R.id.linearLayout1); 
    } 

    public void initRadioGroup() 
    { 
     radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 

     redRadioButton = (RadioButton) findViewById(R.id.redRadioButton); 
     greenRadioButton = (RadioButton) findViewById(R.id.greenRadioButton); 
     blueRadioButton = (RadioButton) findViewById(R.id.blueRadioButton); 

     redRadioButton.setOnCheckedChangeListener(this); 
     greenRadioButton.setOnCheckedChangeListener(this); 
     blueRadioButton.setOnCheckedChangeListener(this); 
    } 

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

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

     switch (buttonView.getId()) 
     { 
      case R.id.redRadioButton: 
       linearLayout.setBackgroundColor(Color.RED); 
       break; 
      case R.id.greenRadioButton: 
       linearLayout.setBackgroundColor(Color.GREEN); 
       break; 
      case R.id.blueRadioButton: 
       linearLayout.setBackgroundColor(Color.BLUE); 
       break; 
     } 

    } 
} 

activity_main.xmlファイルは次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/linearLayout1" 
    android:padding="10dp" 
    android:orientation="vertical"> 

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/radioGroup"> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/redRadioButton" 
      android:text="Red" 
      android:layout_weight="1"/> 
     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/greenRadioButton" 
      android:text="Green" 
      android:layout_weight="1"/> 
     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/blueRadioButton" 
      android:text="Blue" 
      android:layout_weight="1"/> 

    </RadioGroup> 

</LinearLayout> 

答えて

1

ではなく、これが働いsetOnCheckedChangeListener

redRadioButton.setOnClickListener(this); 
greenRadioButton.setOnClickListener(this); 
blueRadioButton.setOnClickListener(this); 

@Override 
public void onClick(View v) { 
     switch (v.getId()) 
     { 
      case R.id.redRadioButton: 
       linearLayout.setBackgroundColor(Color.RED); 
       break; 
      case R.id.greenRadioButton: 
       linearLayout.setBackgroundColor(Color.GREEN); 
       break; 
      case R.id.blueRadioButton: 
       linearLayout.setBackgroundColor(Color.BLUE); 
       break; 
     } 

} 
+0

感謝のsetOnClickListenerを使用しようとすると、今私はこの問題の原因を知っています。 –

関連する問題