cheoly's language study blog

'안드로이드 라디오 버튼'에 해당되는 글 1건

  1. 안드로이드 프로그래밍 라디오 버튼 사용 하기

안드로이드 프로그래밍 라디오 버튼 사용 하기

프로그래밍/안드로이드
반응형
SMALL

오늘은 안드로이드에서 라디오 버튼 사용법에 대해서 알아보겠습니다.


안드로이드라는게 여러가지 방법으로 사용방법이 있는데요..


전 그냥 한 가지 방법만 보여드릴게요..


다른 방법까지 다 하기에는 제가 귀차니즘이 좀 심해서요..ㅋㅋ


그럼 먼저 레이아웃 파일에 라디오 버튼 부터 만들어 볼까요??


전 레이아웃은 linearlayout을 주로 사용해요.


정렬하기 편하잖아요..ㅋㅋㅋ


다른이유 없답니다..^^


라디오버튼은 진짜 간단해요..



이 정도의 코딩만 있으면 2개 왔다 갔다 할 수 있어요..^^



여기 이렇게 라디오 버튼이 생겨 있죠??^^




복붙하고 싶어하시는 분들을 위해서 캡쳐뿐아닌 코드까지 올려드리죠..ㅋㅋㅋ


<LinearLayout

    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:text="주택"
android:id = "@+id/option1"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="15dp"
android:text="상가/오피스텔"
android:layout_marginLeft="10dp"
android:id="@+id/option2"
/>
</RadioGroup>
</LinearLayout>

이렇게 했으면 이제 실제 코딩이 들어가야 할 곳을 건드려 볼까요??^^

RadioButton opt1 = (RadioButton) findViewById(R.id.option1);
RadioButton opt2 = (RadioButton) findViewById(R.id.option2);

이렇게 라디오 버튼을 선언하고 초기화해주죠.. 레이아웃이랑 맞추고요.


opt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "주택 선택됨", Toast.LENGTH_SHORT).show();
house_flag = 0;
}
});
opt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "상가/오피스텔 선택됨", Toast.LENGTH_SHORT).show();
house_flag = 1;
}
});
이렇게 하면 되요..^^
간단하죠???ㅋㅋㅋㅋㅋㅋ


반응형
LIST