본문 바로가기
안드로이드

안드로이드스튜디오 RadionButton, TextView

by Enhydra lutris 2022. 10. 11.
package com.example.androidstudioapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {

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

        RadioButton r1 = (RadioButton) findViewById(R.id.radioButton1);
        RadioButton r2 = (RadioButton) findViewById(R.id.radioButton2);

        r1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                ImageView iv = (ImageView) findViewById(R.id.imageView);
                if (b){
                    switch (compoundButton.getId()){
                        case R.id.radioButton1:
                            iv.setImageResource(R.drawable.undertheseaotter);
                            break;

                        case R.id.radioButton2:
                            iv.setImageResource(R.drawable.seaotter);
                            break;
                    }
                }
            }
        });

        r2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                ImageView iv = (ImageView) findViewById(R.id.imageView);
                if (b){
                    switch (compoundButton.getId()){
                        case R.id.radioButton1:
                            iv.setImageResource(R.drawable.undertheseaotter);
                            break;

                        case R.id.radioButton2:
                            iv.setImageResource(R.drawable.seaotter);
                            break;
                    }
                }
            }
        });
    }
}

라디오 버튼 사용

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:onClick="OnClickButton"
    tools:context=".MainActivity">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="242dp"
        android:layout_marginTop="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/seaotter" />



    <RadioGroup
        android:layout_width="189dp"
        android:layout_height="114dp"
        app:layout_constraintBottom_toBottomOf="parent"
        tools:layout_editor_absoluteX="108dp">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

이미지명을 변수이름처럼 써야됨

라디오 그룹에 라디오버튼을 삽입해야함

'안드로이드' 카테고리의 다른 글

안드로이드스튜디오 간단한 계산기 만들기  (0) 2022.10.04

댓글