radioGroup 리스너 예제 - 2014.04.01

MainActivity.java
package com.example.project4_1_pet;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	TextView tv1, tv2;
	CheckBox start_chkBox;
	RadioGroup rGroup;
	RadioButton rb1, rb2, rb3;
	ImageView imgView;
	Button btn, btnQuit, btnReturn;
	Switch switchBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setTitle("yeongjun");

		switchBtn = (Switch) findViewById(R.id.switchBtn);
		tv1 = (TextView) findViewById(R.id.tv1);
		tv2 = (TextView) findViewById(R.id.tv2);
		rGroup = (RadioGroup) findViewById(R.id.RGroup);
		rb1 = (RadioButton) findViewById(R.id.rb1);
		rb2 = (RadioButton) findViewById(R.id.rb2);
		rb3 = (RadioButton) findViewById(R.id.rb3);
		imgView = (ImageView) findViewById(R.id.imgView);
		btnQuit = (Button) findViewById(R.id.btnQuit);
		btnReturn = (Button) findViewById(R.id.btnReturn);

		switchBtn
				.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton cb,
							boolean isChecking) {
						if (isChecking) {
							Toast.makeText(getApplicationContext(),
									"isChecked", Toast.LENGTH_SHORT).show();
							tv2.setVisibility(1);
							rGroup.setVisibility(1);
							imgView.setVisibility(1);
						} else {
							Toast.makeText(getApplicationContext(),
									"isNotChecked", Toast.LENGTH_SHORT).show();

							tv2.setVisibility(4);
							rGroup.setVisibility(4);
							imgView.setVisibility(4);
						}

					}
				});

		rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb1:
					imgView.setImageResource(R.drawable.dog);
					break;
				case R.id.rb2:
					imgView.setImageResource(R.drawable.cat);
					break;
				case R.id.rb3:
					imgView.setImageResource(R.drawable.rabbit);
					break;
				default:
					Toast.makeText(getApplicationContext(),
							"radio btn select plz", Toast.LENGTH_SHORT).show();
				}
			}
		});

		btnQuit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		btnReturn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				tv2.setVisibility(4);
				rGroup.setVisibility(4);
				imgView.setVisibility(4);

				switchBtn.setChecked(false);
			}
		});
	}
}


main.xml


+ Recent posts