2024/10/31 app

윤수환·2024년 10월 31일

android app

목록 보기
5/8

LinearLayout
디자인 - 체크박스 3개, 버튼 1개 만들고 margin(외부)으로 띄우기
체크박스, 버튼 객체 만들고 연결
onclick 설정 onclick에서 이름 설정해주고 전구 눌러서 만들기

체크박스

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="안드로이드"
        android:textColor="#875FCF" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="JAVA"
        android:textColor="#56A8EA" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Kotlin" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:onClick="onResult"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/mom" />
</LinearLayout>
package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;

import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox ck1, ck2, ck3;
    Button btnClick;
    ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ck1 = (CheckBox) findViewById(R.id.checkBox);
        ck2 = (CheckBox) findViewById(R.id.checkBox2);
        ck3 = (CheckBox) findViewById(R.id.checkBox3);
        btnClick=(Button) findViewById(R.id.button);
        img = (ImageView) findViewById(R.id.imageView);

        ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    img.setVisibility(View.VISIBLE);
                else
                    img.setVisibility(View.INVISIBLE);
            }
        });


    }

    public void onResult(View view) {
        String str = "";
        if(ck1.isChecked() == true)
            str = str + "안드로이드 ";
        if(ck2.isChecked() == true)
            str = str + "JAVA ";
        if(ck3.isChecked() == true)
            str = str + "Kotlin ";

        Toast.makeText(this, str + "를 선택", Toast.LENGTH_SHORT).show();
    }
}

토글 버튼

<?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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_weight="1"
            android:background="@drawable/togglebutton_off"
            android:text="ToggleButton" />

        <ToggleButton
            android:id="@+id/toggleButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:layout_weight="1"
            android:background="@drawable/togglebutton_on"
            android:checked="true"
            android:text="ToggleButton" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/light_turn_off" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/light_turn_on" />
    </LinearLayout>
</LinearLayout>
package com.example.radio2;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    ToggleButton tb1, tb2;
    ImageView l_img, r_img;

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

        tb1 = (ToggleButton) findViewById(R.id.toggleButton);
        tb2 = (ToggleButton) findViewById(R.id.toggleButton2);
        l_img = (ImageView) findViewById(R.id.imageView);
        r_img = (ImageView) findViewById(R.id.imageView2);

        tb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    tb1.setBackgroundResource(R.drawable.togglebutton_on);
                    l_img.setImageResource(R.drawable.light_turn_on);
                }
                else {
                    tb1.setBackgroundResource(R.drawable.togglebutton_off);
                    l_img.setImageResource(R.drawable.light_turn_off);
                }
            }
        });
        tb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    tb2.setBackgroundResource(R.drawable.togglebutton_on);
                    r_img.setImageResource(R.drawable.light_turn_on);
                }
                else {
                    tb2.setBackgroundResource(R.drawable.togglebutton_off);
                    r_img.setImageResource(R.drawable.light_turn_off);
                }
            }
        });
    }
}

gravity - 내부 정렬
layout gravity 두개 차이 알아두기

<?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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Switch
        android:id="@+id/switch1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="숨김"
        android:textColor="#D17979"
        android:textColorHighlight="#C34040"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Switch
        android:id="@+id/switch2"
        android:layout_width="218dp"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="진하게"
        android:textColor="#3F51B5"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Switch
        android:id="@+id/switch3"
        android:layout_width="217dp"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="색상"
        android:textColor="#4CAF50"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="안드로이드 프로그램"
        android:textSize="20sp" />
</LinearLayout>
package com.example.aswitch;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Switch sw1, sw2, sw3;
    TextView txt;

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

        sw1 = (Switch) findViewById(R.id.switch1);
        sw2 = (Switch) findViewById(R.id.switch2);
        sw3 = (Switch) findViewById(R.id.switch3);
        txt = (TextView) findViewById(R.id.textView);

        sw1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    txt.setVisibility(View.VISIBLE);
                else
                    txt.setVisibility(View.INVISIBLE);
            }
        });
        sw2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    txt.setTypeface(null, Typeface.BOLD);
                else
                    txt.setTypeface(null,Typeface.NORMAL);
            }
        });
        sw3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                    txt.setTextColor(Color.GREEN);
                else
                    txt.setTextColor(Color.BLUE);
            }
        });
    }
}

0개의 댓글