<?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="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/dolly" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textColor="#cc0000"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ToggleButton
            android:id="@+id/toogleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ToggleButton
            android:id="@+id/toogleButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
package com.gusfree;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class LayoutActivity extends Activity 
					implements OnClickListener{
    TextView textView; //클래스 변수 선언
    ImageView imageView;
    ToggleButton toggle1, toggle2;
    
    @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frame);
        
        textView=(TextView) findViewById(R.id.textView1);
        imageView = (ImageView) findViewById(R.id.imageView1);
        toggle1=(ToggleButton) findViewById(R.id.toogleButton1);
        toggle2=(ToggleButton) findViewById(R.id.toogleButton2);
       
        toggle1.setOnClickListener(this);
        toggle2.setOnClickListener(this);
        
        MyListener myListener=new MyListener();
        //토글버튼의 Checked가 Change 될때 호출되는 Listener
        toggle1.setOnCheckedChangeListener(myListener);
        toggle2.setOnCheckedChangeListener(myListener);
    } 
    
    class MyListener implements OnCheckedChangeListener{
    	
    	//파라미터1 : 나를 부른뷰.. 파라미터2: 뷰의 체크상태
		@Override
		public void onCheckedChanged(
				CompoundButton v, boolean checked) {
			if(v==toggle1){ 
				if(checked) imageView.setVisibility(View.VISIBLE);
				else imageView.setVisibility(View.GONE);
			}
			// 토글버튼2의 체크상태에 따라 textView가 보였다 안보였다
			else if(v==toggle2){
				if(checked)textView.setVisibility(View.VISIBLE);
				else  textView.setVisibility(View.GONE);
			}
			
		}
    	
    }
    
    /*Listener의 특징 : 파라미터로 리스너를 호출한 뷰가
     * 넘어옵니다     */
	@Override
	public void onClick(View v) {	
		
		/*if(v == toggle1)	{	
			textView.setText("토글버튼1 눌렀다");
			imageView.setVisibility(View.VISIBLE); //static 필드
		}
		else if(v == toggle2){
			textView.setText("토글버튼2 눌렀다");
			imageView.setVisibility(View.INVISIBLE); //static 필드
		}*/
	}
	
	
	
	class A implements OnClickListener{
		
		@Override
		public void onClick(View arg0) {
			
			
		}
		
	}
}







+ Recent posts