3.22
安卓radiobutton
• 所花时间:5
• 代码行数:289
• 博客容量:1
• 代码如下:
package com.example.chapter06;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class radioButton extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private TextView tv_alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);
RadioGroup radioGroup =findViewById(R.id.gender);
tv_alert= findViewById(R.id.tv_alert);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.male){
tv_alert.setText("你是一个帅哥");
}else if (checkedId==R.id.female){
tv_alert.setText("你是一个美女");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".radioButton"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/gender"
>
<RadioButton
android:id="@+id/male"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="female"/>
</RadioGroup>
<TextView
android:id="@+id/tv_alert"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
浙公网安备 33010602011771号