<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/layout" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
package com.listener;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;
public class ListenerActivity extends Activity {
LinearLayout layout;
TextView textView;
EditText editText;
CheckBox checkBox;
RadioGroup radioGroup;
ToggleButton toggleButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout=(LinearLayout) findViewById(R.id.layout);
textView=(TextView) findViewById(R.id.textView1);
editText = (EditText) this.findViewById(R.id.editText1);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
/*체크박스&토글버튼&라디오그룹 : CheckedChange
* 라디오그룹 : ItemSeleced
* 텍스트뷰 : ClickListener
* layout : ClickListener
* editText : addTextWatcher* */
/*리스너 클래스들의 객체를 만들고
뷰에 만든 리스너 객체를 셋팅하세요 */
/*checkBox.setOnCheckedChangeListener(new CheckedChangeListener());
toggleButton.setOnCheckedChangeListener(new CheckedChangeListener());
radioGroup.setOnCheckedChangeListener(new RadioListener());
editText.addTextChangedListener(new EditListener());
layout.setOnClickListener(new ClickListener());
textView.setOnClickListener(new ClickListener());*/
toggleButton.setOnCheckedChangeListener(new T());
checkBox.setOnCheckedChangeListener(new T());
radioGroup.setOnCheckedChangeListener(new RG());
editText.addTextChangedListener(new E());
}
class E implements TextWatcher{
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textView.setText(s);
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
}
class RG implements android.widget.RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
layout.setBackgroundColor(Color.WHITE);
break;
case R.id.radio1:
layout.setBackgroundColor(Color.BLACK);
break;
case R.id.radio2:
layout.setBackgroundColor(Color.GRAY);
break;
}
}
}
class T implements OnCheckedChangeListener{
@Override//토글버튼
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(buttonView==toggleButton){
}
if(isChecked){
layout.setBackgroundResource(R.drawable.sosi);
}else{
layout.setBackgroundResource(R.drawable.sosii);
}
}
/*//textView, layout
class ClickListener implements OnClickListener{
@Override
public void onClick(View v) {
}
}
//toggleButton, checkBox 용
class CheckedChangeListener
implements OnCheckedChangeListener{
// 개똥이
@Override
public void onCheckedChanged(
CompoundButton buttonView,
boolean isChecked) {
}
}
//radioGroup 용
class RadioListener implements
android.widget.RadioGroup.OnCheckedChangeListener{
//한국의 대구에 사는 개똥이
@Override
public void onCheckedChanged(RadioGroup group,
int checkedId) {
}
}
//editText용
class EditListener implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}*/
}
}