• 安卓选项按钮



    复选按钮CheckBox

    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    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);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    <resources>
        <string name="app_name">EX2_11string>
        <string name="hello">选择播放的歌曲string>
        <string name="one">荷塘月色----凤凰传奇string>
        <string name="two">白狐----陈瑞string>
        <string name="three">青花瓷----周杰伦string>
        <string name="btn">请选择选项值string>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    单选按钮

    <resources>
    
    
    
    
    
    
        <string name="hello">请输入您的姓名string>
        <string name="app_name">ex2_12string>
        <string name="boy">string>
        <string name="girl">string>
    resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    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);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    HJ17 坐标移动----牛客刷题
    Spring MVC各组件近距离接触--下下--05
    【Kubernetes系列】工作负载资源之Deployment
    腾讯内部最通俗易懂的项目管理PPT
    二叉树与堆
    ArcGIS属性域和子类型
    如何用蓝牙实现无线定位(四)--远程定位显示
    golang 基础 —— 字符串 与 int 、int64 互转
    6年,我从手工测试到测试开发,写给即将进入或者正在做测试的你...
    JUC 并发编程学习
  • 原文地址:https://blog.csdn.net/weixin_51422230/article/details/127707019