본문 바로가기

Android

버튼 - 토글(toggle)

Activity_main.xml

 

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

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

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

 

    <ToggleButton

        android:id="@+id/tb"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textOn="선택"

        android:textOff="선택안됨"/>

 

</RelativeLayout>

 

 

MainActivity.java

 

package com.example.testex26;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.widget.CompoundButton;

import android.widget.Toast;

import android.widget.ToggleButton;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           ToggleButton tb = (ToggleButton)findViewById(R.id.tb);

           tb.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();

                     }

                }

           });

          

     }

 

     @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' 카테고리의 다른 글

이미지 뷰(ImageView) - 이미지 버튼(ImageButton)  (0) 2013.03.28
이미지 뷰(imageView) - 확대 모드  (0) 2013.03.28
버튼 - 라디오버튼  (1) 2013.03.28
버튼 - 콤보박스  (0) 2013.03.28
텍스트뷰(textview) - 기본속성  (0) 2013.03.28