본문 바로가기

Android

버튼 - 콤보박스

Activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    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=".MainActivity" >

 

    <CheckBox

        android:id="@+id/cb1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:checked="true"

        android:text="check1" />

 

    <CheckBox

        android:id="@+id/cb2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="check2" />

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.testex24;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           CheckBox cb1 = (CheckBox)findViewById(R.id.cb1);

           CheckBox cb2 = (CheckBox)findViewById(R.id.cb2);

          

           cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

               

                @Override

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                     // TODO Auto-generated method stub

                     if(isChecked){

                           Toast.makeText(MainActivity.this, "체크되었음", Toast.LENGTH_SHORT).show();

                     } else {

                           Toast.makeText(MainActivity.this, "체크해제되었음", Toast.LENGTH_SHORT).show();

                     }

                }

            

           });

          

           cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

               

                @Override

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                     // TODO Auto-generated method stub

                    

                }

            

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 

 

MainActivity.java 변형 하나의 리스너로 만들기

 

package com.example.testex24;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);

           CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);

 

           cb1.setOnCheckedChangeListener(checkChangeListener);

           cb2.setOnCheckedChangeListener(checkChangeListener);

          

     }

 

 

     private CompoundButton.OnCheckedChangeListener checkChangeListener = new CompoundButton.OnCheckedChangeListener() {

 

           @Override

           public void onCheckedChanged(CompoundButton buttonView,

                     boolean isChecked) {

                // TODO Auto-generated method stub

                switch (buttonView.getId()) {

                case R.id.cb1:

                     if (isChecked) {

                           Toast.makeText(MainActivity.this

"cb1 체크 되었음",Toast.LENGTH_SHORT).show();

                     } else {

                           Toast.makeText(MainActivity.this

"cb1 체크 해제되었음",Toast.LENGTH_SHORT).show();

                     }

                     break;

                case R.id.cb2:

                     if (isChecked) {

                           Toast.makeText(MainActivity.this

"cb2 체크 되었음",Toast.LENGTH_SHORT).show();

                     } else {

                           Toast.makeText(MainActivity.this

"cb2 체크 해제되었음",Toast.LENGTH_SHORT).show();

                     }

                     break;

                default:

                     break;

                }

           }

     };

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}

 



'Android' 카테고리의 다른 글

버튼 - 토글(toggle)  (0) 2013.03.28
버튼 - 라디오버튼  (1) 2013.03.28
텍스트뷰(textview) - 기본속성  (0) 2013.03.28
이벤트 예제 - 클릭  (1) 2013.03.28
입력 - 터치 실습  (0) 2013.03.28