Android/2. 기본 위젯
Day 108 : 기본 위젯 활용하기
pancakemaker
2022. 3. 24. 11:47
1. 컴파운드버튼 - Button 클래스의 하위 클래스
(1) 체크박스
//체크박스 변수 선언
CheckBox myCheck;
//변수에 체크박스 위젯 대입
myCheck = (CheckBox) findViewById(R.id.android);
//체크박스가 변경될 때 동작하는 클래스 정의
myCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//동작 내용 작성
}
});
- 예시
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/android"
android:text="안드로이드폰"
android:checked="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iphone"
android:text="아이폰"
android:checked="true"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/window"
android:text="윈도우폰"
android:checked="false"/>
</LinearLayout>
(2) 스위치과 토글버튼
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false" />
</LinearLayout>
(3) 라디오버튼과 라디오그룹
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="남성"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여성"/>
</RadioGroup>
</LinearLayout>
2. 이미지뷰와 이미지버튼
<?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"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<ImageView
android:layout_width="300dp"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"/>
<ImageView
android:layout_width="300dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher"/>
</LinearLayout>