-버튼, 라디오버튼 만들어서 클릭 시 색변환
-버튼 RED누르면 라디오버튼 RED도 동시에 클릭됨
<MainActivity>
package com.example.graphic;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.text.BreakIterator;
public class MainActivity extends AppCompatActivity {
LinearLayout layout;
RadioGroup radioGroup;
RadioButton radioRed, radioGreen, radioBlue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = findViewById(R.id.main);
radioRed = findViewById(R.id.radioRed);
radioGreen = findViewById(R.id.radioGreen);
radioBlue = findViewById(R.id.radioBlue);
radioGroup = findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioRed:
layout.setBackgroundColor(Color.RED);
radioRed.setTextColor(Color.RED);
radioGreen.setTextColor(Color.BLACK);
radioBlue.setTextColor(Color.BLACK);
break;
case R.id.radioGreen:
layout.setBackgroundColor(Color.GREEN);
radioRed.setTextColor(Color.BLACK);
radioGreen.setTextColor(Color.GREEN);
radioBlue.setTextColor(Color.BLACK);
break;
case R.id.radioBlue:
layout.setBackgroundColor(Color.BLUE);
radioRed.setTextColor(Color.BLACK);
radioGreen.setTextColor(Color.BLACK);
radioBlue.setTextColor(Color.BLUE);
break;
}
}
});
}
public void onClick(View view) {
switch (view.getId()){
case R.id.btnRed:
layout.setBackgroundColor(Color.RED);
radioRed.setChecked(true);
break;
case R.id.btnGreen:
layout.setBackgroundColor(Color.GREEN);
radioGreen.setChecked(true);
break;
case R.id.btnBlue:
layout.setBackgroundColor(Color.GREEN);
radioBlue.setChecked(true);
break;
}
}
}
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal">
<Button
android:id="@+id/btnRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:textColor="#F15252"
android:text="RED" />
<Button
android:id="@+id/btnGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:textColor="#54B354"
android:text="GREEN" />
<Button
android:id="@+id/btnBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:textColor="#101055"
android:text="BLUE" />
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<RadioButton
android:id="@+id/radioRed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<RadioButton
android:id="@+id/radioGreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
<RadioButton
android:id="@+id/radioBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
</RadioGroup>
</LinearLayout>
'Android' 카테고리의 다른 글
안드로이드 - 영화 리스트 만들기 (0) | 2020.11.04 |
---|---|
안드로이드 - toast 클릭 시 클릭한 것 뜨게하기 (여러 개 가능) (0) | 2020.11.04 |
안드로이드 - Custom Dialog (0) | 2020.10.30 |
안드로이드 - 5일차 (DatePickerDialog TextClock) (0) | 2020.10.30 |
안드로이드 수업 3일차 - 날짜, 온도변환 (0) | 2020.10.28 |