안드로이드

 

 

 

class MainActivity 

package org.androidtown.mywidget;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        MultiCheckBox multiChecBox =(MultiCheckBox)findViewById(R.id.multiCheck);

        //부분화면 객체 생성 이벤트 처리리
       multiChecBox.setOnMultiChangeListener(new MultiCheckBox.OnMultiChangeListner() {

            @Override
            public void onMultiChanged(boolean isFirstChecked, boolean isSecondChecked) {

                Toast.makeText(getApplicationContext(), "첫번째 체크는 :" + isFirstChecked
                        + " 두번째 체크는 : " + isSecondChecked,
                        Toast.LENGTH_SHORT
                ).show();
            }
        });

    }



}

 

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.mywidget.MainActivity">

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:id="@+id/button" />

    <org.androidtown.mywidget.MultiCheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_marginTop="73dp"
        android:id="@+id/multiCheck"
        android:layout_centerHorizontal="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

 

 

class MultiCheckBox

package org.androidtown.mywidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

/**
 * Created by choi on 2017-04-09.
 */

public class MultiCheckBox extends LinearLayout {

    // 사용자가 만드 인터페이스
    // 메모리 객체화 후 메인 엑티비티에서 버튼 클릭이 동작하기위한 동작 설정 인터페이스
    // 내부 인터페이스
    public interface OnMultiChangeListner{
        public void onMultiChanged(boolean isFirstChecked, boolean isSecondChecked);
    }

    OnMultiChangeListner listner;

    CheckBox checkBox1, checkBox2;

    public void setOnMultiChangeListener (OnMultiChangeListner listner){
        this.listner=listner;
    }

    public MultiCheckBox(Context context) {
        super(context);
        init(context);
    }



    public MultiCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    private void init(Context context){
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.multi_checkbox, this, true);

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


        // inflater 과정에서 아래 버튼 동작들이 메모리에 올라간다. 이것을 메인 엑티비티에서
        // 제어하기위해  인터페이스 를 만들었다. 따라서  메인 클래스에서 익명클래스를 이용해서
        //체크박스 제어가 가능하도록 한다. 이렇게 제어하는 방식은  프레임레이아웃 과 유사하다.

        //첫번재 선택박스스
       checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(listner!=null){ //리스너가 설정 되어 있다면.
                    //첫번째와 두번째 박스 설정값 전달
                    listner.onMultiChanged(isChecked, checkBox2.isChecked());
                }
            }
        });

        //두번재 선택박스스
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(listner!=null){ //리스너가 설정 되어 있다면.
                    //첫번째와 두번째 박스 설정값 전달
                    listner.onMultiChanged(checkBox1.isChecked(), isChecked);
                }
            }
        });
    }




}








 

R.layout.multi_checkbox

<?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="wrap_content"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    >

    <CheckBox
        android:text="바나나"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox1"
        android:layout_gravity="center_vertical|center_horizontal"
         />

    <CheckBox
        android:text="망고"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox2"
        android:layout_gravity="center_vertical|center_horizontal"
         />


</LinearLayout>

 

 

 

 

 

63강

 

 

 

64강

 

 

about author

PHRASE

Level 60  머나먼나라

도박을 즐기는 모든 인간은, 불확실한 것을 얻기 위해서 확실한 것을 걸고 내기를 한다. -파스칼

댓글 ( 4)

댓글 남기기

작성