관리 메뉴

드럼치는 프로그래머

[안드로이드] 제8강좌 - Button만들기 본문

★─Programing/☆─Android

[안드로이드] 제8강좌 - Button만들기

드럼치는한동이 2011. 5. 30. 10:11

============================================================================================================================================

@@@ 체크박스와 토크버튼 @@@

1. Check Box - ToggleButton과 TextView가 합성되어 만들어짐 (체크되고 안되었때)

2. Toggle_box - 꺼짐/켜짐 상태를 표시하여주는 버튼





package com.android.box;

 

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;


public class Androidbox extends Activity {
    /** Called when the activity is first created. */
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final CheckBox check_Button = (CheckBox) findViewById(R.id.checkbox);
        check_Button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v){
          TextView tv = (TextView)findViewById(R.id.checkbox);
          tv.setText(check_Button.isChecked()?
             "This option is checked":"This option is not checke");
         }
        });
       
        final ToggleButton toggle_button = (ToggleButton) findViewById(R.id.toggle_button);
        toggle_button.setOnClickListener(new View.OnClickListener() {      //onclick으로 한번 클릭시 변경가능하게
         public void onClick(View v){


          TextView tv=(TextView) findViewById(R.id.text_feature);//토글버튼 클릭시 글자 변경 위해 textview 변경
          tv.setText(toggle_button.isChecked()?
            "This feature is on" : "This feature is off");
          
          LinearLayout lv=(LinearLayout) findViewById(R.id.linear);   //토글버튼 클릭시 background를 변경하기 위해 linearlayout을 변경
          lv.setBackgroundColor(toggle_button.isChecked()?
            Color.BLUE:Color.RED);          
         }         
  });          
    }
}

 

@@@ xml @@@

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"  // background를 변경하기 위하여 id부여
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   
    >
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <CheckBox
     android:id="@+id/checkbox"      // checkbox를 변경하기 위하여 id부여
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Check me?" />  
   

    <ToggleButton
     android:id="@+id/toggle_button"       //
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
   

     <TextView
     android:id="@+id/text_feature"      //
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="center_vertical"
     android:text="Toggle me" />
      
</LinearLayout>

============================================================================================================================================

@@@ 라디오버튼과 라디오그룹 @@@

1. 라디오 버튼 - 체크박스와 비슷하지만 여러가지 중에 하나만 선택 됨

  라디오 버튼은 라디오 그룹 안에다가 만든다.

  이벤트 리스너는 라디오 그룹이 가지고 있다.

  onclick을 라디오 그룹에 부여하여야 버튼이 눌릴때마다 이벤트가 발생하는 것을 막을 수 있다.

  group.setOnCheckedChangeListener() 그룹이 가지고 있는 리스너  .... view 아님

          public void onChesckedChanged(라디오그릅  명, 변수형 )




@@@@ java @@@@

package com.android.box;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

 

public class Androidbox extends Activity {  //Activity 안에 class 선언
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final Button basic_button=(Button)findViewById(R.id.basic_button);
           basic_button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                       Toast.makeText(Androidbox.this,"Button Clicked", Toast.LENGTH_SHORT).show();
               }
         });
       
        final ImageButton image_button=(ImageButton) findViewById(R.id.image_button);
            image_button.setOnClickListener(new View.OnClickListener() {
              @Override
                 public void onClick(View v) {
                        Toast.makeText(Androidbox.this, "Image button Clixked", Toast.LENGTH_SHORT).show();
                 }
         });     
       
        final CheckBox check_Button = (CheckBox)findViewById(R.id.checkbox);
           check_Button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                 TextView tv =(TextView)findViewById(R.id.checkbox);
                       tv.setText(check_Button.isChecked()?"This option is checked":"This option is not check");
             }
        });
       
        final ToggleButton toggle_button = (ToggleButton)findViewById(R.id.toggle_button);
            toggle_button.setOnClickListener(new View.OnClickListener() {
               public void onClick(View v) {
                   TextView tv=(TextView)findViewById(R.id.text_feature);
                    tv.setText(toggle_button.isChecked()?"This feature is on":"This feature is off");  //if문의 변형
                    LinearLayout lv=(LinearLayout)findViewById(R.id.linear);
                    lv.setBackgroundColor(toggle_button.isChecked()?Color.BLUE:Color.RED);

            }
        });
       
        final RadioGroup group =(RadioGroup)findViewById(R.id.RadioGroup01);
          group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
             public void onCheckedChanged(RadioGroup group, int checkedId) {
               TextView tv=(TextView)findViewById(R.id.TextView01);
               if(checkedId!=-1){   //기본
                   RadioButton rb=(RadioButton)findViewById(checkedId);
                     if(rb!=null){
                         tv.setText("선택 : "+rb.getText());
                     }
               }
               else{
                 tv.setText("choose 1");
              }
           }
        });
       
        final Button clear_choice = (Button) findViewById(R.id.Button01);
        clear_choice.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
              RadioGroup group=(RadioGroup)findViewById(R.id.RadioGroup01);
              if(group !=null){
                 group.clearCheck();  //All Clear 버튼을 만듬
              }
          }
      });
    }
}

 
@@@ xml @@@

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/linear"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView 
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:text="@string/hello"    />
    <Button
     android:id="@+id/basic_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Basic Button" />
    <ImageButton
     android:id="@+id/image_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/droid"/>
    <CheckBox
     android:id="@+id/checkbox"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Check me?" />
    <ToggleButton
     android:id="@+id/toggle_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Toggle"
     android:textOn="Disabled"
     android:textOff="Enabled" />
    <TextView
     android:id="@+id/text_feature"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="center_vertical"
     android:text="Toggle me!" />
 </LinearLayout>
 <LinearLayout
  android:id="@+id/LinearLayout01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:gravity="center" >
     <RadioGroup
     android:id="@+id/RadioGroup01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" >
     <RadioButton
      android:id="@+id/RadioButton01"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Option01" />
  <RadioButton
      android:id="@+id/RadioButton02"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Option02" />
     <RadioButton
      android:id="@+id/RadioButton03"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Option03" />
     <RadioButton
      android:id="@+id/RadioButton04"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Option04" />
    </RadioGroup>
    <TextView
     android:id="@+id/TextView01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="하나를 선택하시오" />
    <Button
     android:id="@+id/Button01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Clear All" >
     </Button>
   </LinearLayout>
</LinearLayout>

============================================================================================================================================

@@@ Digital & Analog Clock @@@

   

@@@ xml @@@ 

    <DigitalClock
     android:id="@+id/DigitalClock01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <AnalogClock
     android:id="@+id/AnalogClock01"
     android:layout_width="wrap_content"
     android:layout_height="100px" />
    <DatePicker
     android:id="@+id/DatePicker"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />

Comments