액티비티의 RadioButton을 클릭하면 액티비티의 배경색이 변경되는 안드로이드 애플리케이션을 만들었습니다. 활동의 배경 색상을 변경해야 RadioButtons를 중 어느 하나의 클릭에 안드로이드에서 레이아웃의 배경을 동적으로 변경하면 제대로 작동하지 않습니다.
그래서 우리가 가지고 3 RadioButtons를 : 응용 프로그램을 열기에 :
나는 이미지의 도움으로 내 질문을 정교화하고있어 각 색상.
=> 라디오 버튼을 처음 클릭하면 배경색이 제대로 반영됩니다. 예 :
녹색에 녹색 배경 색상 변경을 클릭 한 후.
=>하지만 그 중 하나를 클릭 할 때마다 배경색이 즉시 반영되지 않습니다. 예를 들면 :
나는 블루를 클릭하지만, 배경 색상은 여전히 녹색이다.
=> 다른 라디오 버튼을 클릭하면 이전에 클릭 한 라디오 버튼의 배경색이 활동에 반영됩니다. 예 :
내가 빨간색을 클릭 한 후 나는 파란색으로 배경색을 얻었다.
그럼 어떻게이 문제를 해결할 수 있습니까? 라디오 버튼을 체크 한 직후 배경색을 변경하고 싶습니다.
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>
감사의
setOnClickListener
를 사용하는 이제이 문제의 원인을 알았습니다. –