본문 바로가기

Android

버튼 - 라디오버튼

Activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <RadioGroup

        android:id="@+id/rg"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:checkedButton="@+id/rb2" >

 

        <RadioButton

            android:id="@+id/rb1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="radio1" />

 

        <RadioButton

            android:id="@+id/rb2"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="radio2" />

 

        <RadioButton

            android:id="@+id/rb3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="radio3" />

    </RadioGroup>

 

</LinearLayout>

 

 

MainActivity.java

 

package com.example.testex25;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.view.Menu;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           RadioGroup rg = (RadioGroup) findViewById(R.id.rg);

           rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

 

                @Override

                public void onCheckedChanged(RadioGroup group, int checkedId) {

                     // TODO Auto-generated method stub

                    

                    RadioButton rg = (RadioButton)findViewById(checkedId);

                     Toast.makeText(MainActivity.this, rg.getText() + "체크", Toast.LENGTH_SHORT).show();

                    

//                   switch (checkedId) {

//                   case R.id.rb1:

//                         Toast.makeText(MainActivity.this, 

"rb1 체크",Toast.LENGTH_SHORT).show();

//                         break;

//                   case R.id.rb2:

//                         Toast.makeText(MainActivity.this, 

"rb2 체크",Toast.LENGTH_SHORT).show();

//                         break;

//                   case R.id.rb3:

//                         Toast.makeText(MainActivity.this, 

"rb3 체크",Toast.LENGTH_SHORT).show();

//                         break;

//

//                   default:

//                         break;

//                   }

 

                }

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}




 

 

'Android' 카테고리의 다른 글

이미지 뷰(imageView) - 확대 모드  (0) 2013.03.28
버튼 - 토글(toggle)  (0) 2013.03.28
버튼 - 콤보박스  (0) 2013.03.28
텍스트뷰(textview) - 기본속성  (0) 2013.03.28
이벤트 예제 - 클릭  (1) 2013.03.28