PROGRAMMING

버튼-백그라운드컬러 본문

Android

버튼-백그라운드컬러

Raccoon2125 2020. 11. 3. 10:06
package com.example.a201103_1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    LinearLayout topLayout;
    RadioGroup radioGroup;

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

        topLayout = (LinearLayout) findViewById(R.id.topLayout);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.radioButton:
                        topLayout.setBackgroundColor(Color.YELLOW);
                        break;
                    case R.id.radioButton2:
                        topLayout.setBackgroundColor(Color.CYAN);
                        break;
                    case R.id.radioButton3:
                        topLayout.setBackgroundColor(Color.MAGENTA);
                        break;
                }
            }
        });
    }

    public void onClick(View view) {
        switch(view.getId()){
            case R.id.button:
                topLayout.setBackgroundColor(Color.YELLOW);
                break;
            case R.id.button2:
                topLayout.setBackgroundColor(Color.CYAN);
                break;
            case R.id.button3:
                topLayout.setBackgroundColor(Color.MAGENTA);
                break;
        }
    }
}

<?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:id="@+id/topLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="YELLOW"
            android:textColor="#FFEB3B"
            android:onClick="onClick"
            app:backgroundTint="#2DFFFFFF" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CYAN"
            android:textColor="#00BCD4"
            android:onClick="onClick"
            app:backgroundTint="#22FFFFFF" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MAGENTA"
            android:textColor="#673AB7"
            android:onClick="onClick"
            app:backgroundTint="#25FFFFFF" />
    </LinearLayout>

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

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="YELLOW"
            android:textColor="#FFEB3B" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CYAN"
            android:textColor="#00BCD4" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MAGENTA"
            android:textColor="#673AB7" />
    </RadioGroup>
</LinearLayout>

'Android' 카테고리의 다른 글

ListViewEdit  (0) 2020.11.04
ListView  (0) 2020.11.04
어플 접속화면(feat. 이상형월드컵)  (0) 2020.10.30
이상형월드컵(ver. teacher)  (0) 2020.10.30
AlertDialog  (0) 2020.10.30
Comments