复选按钮CheckBox
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="20sp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/check1"
android:textSize="20sp"
android:text="@string/one"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/check2"
android:textSize="20sp"
android:text="@string/two"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/check3"
android:textSize="20sp"
android:text="@string/three"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:textSize="20sp"
android:text="@string/btn">
</Button>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text=""
android:textSize="20sp">
</TextView>
</LinearLayout>
package com.example.liearlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
CheckBox ch1,ch2,ch3;
Button okBtn;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ch1=findViewById(R.id.check1);
ch2=findViewById(R.id.check2);
ch3=findViewById(R.id.check3);
okBtn=findViewById(R.id.button);
txt=findViewById(R.id.textView2);
okBtn.setOnClickListener(new click());
}
class click implements View.OnClickListener
{
public void onClick(View v){
String str="";
if(ch1.isChecked())str=str+"\n"+ch1.getText();
if(ch2.isChecked())str=str+"\n"+ch2.getText();
if(ch3.isChecked())str=str+"\n"+ch3.getText();
txt.setText("你选择了:"+str);
}
}
}
<resources>
<string name="app_name">EX2_11</string>
<string name="hello">选择播放的歌曲</string>
<string name="one">荷塘月色----凤凰传奇</string>
<string name="two">白狐----陈瑞</string>
<string name="three">青花瓷----周杰伦</string>
<string name="btn">请选择选项值</string>
</resources>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9c6552668970cfbcfa1cedc45fa6b2a2.png)
单选按钮
<resources>
<string name="hello">请输入您的姓名</string>
<string name="app_name">ex2_12</string>
<string name="boy">男</string>
<string name="girl">女</string>
</resources>
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="20sp"/>
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="20sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/boy01"
android:text="@string/boy"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/girl01"
android:text="@string/girl"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myButton"
android:textSize="20sp"
android:text="确定">
</Button>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text02"
android:textSize="20sp">
</TextView>
</LinearLayout>
package com.example.liearlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button okBtn;
TextView txt;
EditText edit;
RadioButton r1,r2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit =findViewById(R.id.edit1);
okBtn=findViewById(R.id.myButton);
txt=findViewById(R.id.text02);
okBtn.setOnClickListener(new mClick());
r1 =findViewById(R.id.boy01);
r2 =findViewById(R.id.girl01);
}
class mClick implements View.OnClickListener
{
public void onClick(View v){
CharSequence str ="",name="";
name = edit.getText();
if(r1.isChecked())
str = r1.getText();
if(r2.isChecked())
str = r2.getText();
txt.setText("您输入的信息为:\n姓名"+name+"\t性别"+str);
}
}
}