728x90
안녕하세요.
오늘은 클릭한 라디오버튼과 클릭하지 않은 라디오버튼의 배경을 다르게 보여줄 수 있도록 xml 파일을 만들어 보도록 하겠습니다.
클릭한 버튼은 검은색 배경에 하얀색 텍스트로, 클릭하지 않은 버튼은 하얀색 배경에 검은색 텍스트로 만들어 보겠습니다.
먼저, 라디오버튼의 배경이 될 xml 파일입니다. (res> drawable 에 radio_button_background.xml 파일 생성)
xml 파일 코드(radio_button_background.xml )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape android:shape="rectangle">
<solid android:color="#000000" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
</item>
<item android:state_checked="false">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
</shape>
</item>
</selector>
라디오버튼의 텍스트 색을 설정하는 xml 파일입니다. (res> color 에 radio_button_text.xml 파일 생성)
xml 파일 코드(radio_button_text.xml )
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#FFFFFF">
</item>
<item android:state_checked="false" android:color="#404040">
</item>
</selector>
레이아웃 xml 파일 코드(main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20dp"
android:text="텍스트" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:button="@null"
android:textColor="@color/radio_button_text"
android:background="@drawable/radio_button_background"
android:text="라디오 버튼1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:button="@null"
android:textColor="@color/radio_button_text"
android:background="@drawable/radio_button_background"
android:text="라디오 버튼2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:button="@null"
android:textColor="@color/radio_button_text"
android:background="@drawable/radio_button_background"
android:text="라디오 버튼3" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:button="@null"
android:textColor="@color/radio_button_text"
android:background="@drawable/radio_button_background"
android:text="라디오 버튼4" />
</RadioGroup>
</LinearLayout>
메인 액티비티 자바 코드
package com.example.test5;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class mainActivity extends AppCompatActivity {
RadioGroup radiogroup;
TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radiogroup=findViewById(R.id.radiogroup);
textView=findViewById(R.id.textView);
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checked_button;
checked_button = findViewById(checkedId);
textView.setText(checked_button.getText().toString());
}
});
}
}
'안드로이드 자바' 카테고리의 다른 글
[JAVA][Android] View Binding으로 레이아웃 뷰 연결하기 (0) | 2024.08.11 |
---|---|
[JAVA][Android] CustomTextWatcher 사용해서 컴마, 원화 표시하기 (0) | 2024.08.10 |
[JAVA][Android] RecyclerView의 간격을 조정하기 (0) | 2024.08.05 |
[JAVA][Android] BottomSheetDialogFragment 에 값 전달하고 받기 (0) | 2024.08.04 |
[JAVA][Android] UTC 시간을 로컬 시간으로 변경하기 (0) | 2024.08.03 |