안드로이드 컨트롤 사용 시리즈 10:라디오 버튼 사용
2479 단어 android사용RadioButton라디오 버튼
이 예는 성별의 선택을 실현하고 남자나 여자를 선택하고 버튼을 누르면 선택한 정보를 표시한다.
주요 사고방식: 한 라디오 그룹 컨트롤에서 두 개의 라디오 버튼 컨트롤을 정의하여 선택한 성별을 나타내는 남자나 여자를 나타낸다.Button 컨트롤을 정의하는 OnClick 이벤트입니다. 이벤트에서 Radio Group 컨트롤에서 Radio Button 컨트롤의 개수를 가져오고 각 Radio Button 컨트롤을 옮겨다니며 컨트롤이 선택된 것을 발견하면 선택한 컨트롤의 내용을 표시합니다.
activity_main.xml 파일:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" :"
/>
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/radiobutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
/>
<RadioButton
android:id="@+id/radiobutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "
/>
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "
/>
</LinearLayout>
MainActivity.java 파일:
<span style="color:#cc33cc;"> </span> private RadioGroup group;
private Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group=(RadioGroup)findViewById(R.id.sex);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
int len=group.getChildCount();
String msgString="";
for(int i=0;i<len;i++){
RadioButton radioButton=(RadioButton)group.getChildAt(i);
if(radioButton.isChecked()){
msgString=radioButton.getText().toString();
break;
}
}
Toast.makeText(MainActivity.this, msgString, 1).show();
}
});
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.