사용하면 좋다...
TextView - OnClick
EditText - .addTextChangeListener(TextWatcher)
CheckBox - .setOnChekcedChangedListener()
RadioGroup - .setOnCheckdChangedListener()
ToggleButton - .setOnChekcedChangedListener()
Button - .setOnClickListener
<?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:background="@drawable/weather" android:id="@+id/ll" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="TextView Demo" android:textColor="#ffffff" android:textSize="13px" android:textStyle="bold" /> <TextView android:id="@+id/textView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:autoLink="web|email|phone|map" android:text="" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="input your name!" > </EditText> <ImageButton android:contentDescription="버튼이미지" android:id="@+id/imageButton1" android:layout_width="10mm" android:layout_height="10mm" android:src="@drawable/weather" /> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ToggleButton android:text="토글버튼" android:textOn="ON" android:textOff="OFF" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="email수신여부" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="남자" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="여자" /> </RadioGroup> </LinearLayout>
package com.gusfree.lisenter2; import android.app.Activity; import android.content.res.Resources; 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.ImageButton; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.ToggleButton; public class Lisetner2Activity extends Activity { /** Called when the activity is first created. */ EditText et1; TextView tv1; TextView tv2; ImageButton ib; ToggleButton tb; ProgressBar pb; CheckBox cb; RadioGroup rg; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et1 = (EditText) findViewById(R.id.editText1); tv1 = (TextView) findViewById(R.id.textView1); tv2 = (TextView) findViewById(R.id.textView2); ib = (ImageButton) this.findViewById(R.id.imageButton1); tb = (ToggleButton) findViewById(R.id.toggleButton1); pb = (ProgressBar) findViewById(R.id.progressBar1); cb = (CheckBox) findViewById(R.id.checkBox1); rg = (RadioGroup) findViewById(R.id.radioGroup1); // event ib.setOnClickListener(new ImageButtonHandler()); tb.setOnCheckedChangeListener(new ToggleButtonHandler()); cb.setOnCheckedChangeListener(new CheckBoxHandler()); rg.setOnCheckedChangeListener(new RadioButtonHandler()); et1.addTextChangedListener(new EditTextHandler()); Resources res=this.getResources(); String tc=res.getString(R.string.hello); tv2.setText(tc); } /*******EditText***************/ public class EditTextHandler implements TextWatcher{ public void afterTextChanged(Editable arg0) { } public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { tv2.setText(arg0); } } /******** RadioGroup *******/ public class RadioButtonHandler implements android.widget.RadioGroup.OnCheckedChangeListener { public void onCheckedChanged(RadioGroup group, int checkedId) { LinearLayout ll=(LinearLayout)findViewById(R.id.ll); if (checkedId == R.id.radio0) { // 남자그림배경 ll.setBackgroundResource(R.drawable.phi); } else if (checkedId == R.id.radio1) { // 여자그림배경 ll.setBackgroundResource(R.drawable.iyou); } } } /********** CheckBox ****************************/ public class CheckBoxHandler implements OnCheckedChangeListener { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { buttonView.setText("email수신여부[체크]"); } else { buttonView.setText("email수신여부[안체크]"); } } } /************ ToggleButton *********************/ public class ToggleButtonHandler implements OnCheckedChangeListener { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { // progress bar pb.setVisibility(View.VISIBLE); } else { pb.setVisibility(View.INVISIBLE); } } } /************* ImageButton ********************/ public class ImageButtonHandler implements OnClickListener { boolean b = false; public void onClick(View v) { /* * Editable android.widget.EditText.getText() */ String str = et1.getText().toString(); tv1.setText(str); et1.setText(""); /** image 변경 **/ b = !b; if (b) { ib.setImageResource(R.drawable.weather); } else { ib.setImageResource(R.drawable.koreanfood); } } } }
'Android > 2012.04월 강좌' 카테고리의 다른 글
4일차 ListView (attributes for item) (3) | 2012.05.01 |
---|---|
4일차 ListViewActivity - Adapter (2) | 2012.05.01 |
3일차 다양한 이벤트 처리(미완성) (0) | 2012.04.30 |
3일차 LinearLayout( frame 중복) (0) | 2012.04.30 |
3일차 FrameLayout 중복 (0) | 2012.04.30 |