单选按钮
在XML里写一组单选按钮(2个)+ 一个文本(用于提示):
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/rdg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rbtn"
android:textSize="25dp"
android:text="男"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RadioButton
android:textSize="25dp"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>
<TextView
android:id="@+id/tv"
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
在Java中编写代码
package com.example.no_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.rdg);
textView = (TextView) findViewById(R.id.tv);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == R.id.rbtn) {
textView.setText("您的性别是:男");
} else {
textView.setText("您的性别是:女");
}
}
});
}
}
运行,点击按钮,在底下提示相应信息:
复选按钮
在XML里写一组复选按钮 + 两个提示文本 + 一个显示输出文本
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择爱好:"
android:textSize="18dp"/>
<CheckBox
android:id="@+id/shuttlecock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球"
android:textSize="18dp"/>
<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"
android:textSize="18dp"/>
<CheckBox
android:id="@+id/pingpong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球"
android:textSize="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您选择的兴趣爱好为:"
android:textSize="22dp"/>
<TextView
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"/>
</LinearLayout>
在Java中编写代码,利用TextView提示哪一个复选框被选择:
package com.example.no_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private TextView hobby;
private String hobbys;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox shuttlecock = (CheckBox) findViewById(R.id.shuttlecock);
CheckBox basketball = (CheckBox) findViewById(R.id.basketball);
CheckBox pingpong = (CheckBox) findViewById(R.id.pingpong);
shuttlecock.setOnCheckedChangeListener(this);
basketball.setOnCheckedChangeListener(this);
pingpong.setOnCheckedChangeListener(this);
hobby = (TextView) findViewById(R.id.hobby);
hobbys = new String();
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String motion = compoundButton.getText().toString();
if (b) {
if (!hobbys.contains(motion)) {
hobbys = hobbys + motion;
hobby.setText(hobbys);
}
} else {
if (hobbys.contains(motion)) {
hobbys = hobbys.replace(motion, "");
hobby.setText(hobbys);
}
}
}
}
运行,在底下提示对应信息:
|