安卓选项按钮


复选按钮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>

在这里插入图片描述

单选按钮

<resources>
<!--    <string name="app_name">EX2_11</string>-->
<!--    <string name="hello">选择播放的歌曲</string>-->
<!--    <string name="one">荷塘月色&#45;&#45;&#45;&#45;凤凰传奇</string>-->
<!--    <string name="two">白狐&#45;&#45;&#45;&#45;陈瑞</string>-->
<!--    <string name="three">青花瓷&#45;&#45;&#45;&#45;周杰伦</string>-->
<!--    <string name="btn">请选择选项值</string>-->
    <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);
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醉卧考场君莫笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值