«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

ITGenerations

안드로이드 앱 만들기2 본문

Univ/안드로이드

안드로이드 앱 만들기2

ITGenerations 2018. 4. 20. 07:37

조건: 체크박스를 선택할 때 마다 버튼의 속성이 설정되도록 프로젝트를 작성하시오.





xml source


<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ryan.study2.MainActivity">


<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enable 속성" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clickable 속성" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="45도 회전" />

<Button
android:layout_marginTop="120dp"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>





java source


package com.example.ryan.study2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {

CheckBox checkBox1, checkBox2,checkBox3;
Button btn;

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



checkBox1 =(CheckBox)findViewById(R.id.checkBox1);
checkBox2 = (CheckBox)findViewById(R.id.checkBox2);
checkBox3 = (CheckBox)findViewById(R.id.checkBox3);

btn = (Button) findViewById(R.id.button);

checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox3.isChecked()==true){
btn.setRotation(45);
} else
btn.setRotation(0);
}
});

checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox1.isChecked()==true){
btn.setEnabled(true);
} else
btn.setEnabled(false);
}
});

checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox2.isChecked()==true){
btn.setClickable(true);
} else btn.setClickable(false);
}
});



}
}