zoukankan      html  css  js  c++  java
  • 第十七篇-使用RadioGroup实现单项选择

    上效果图

    首先进行控件布局,一个textview,6个radiobutton,

    main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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=".MainActivity">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="233dp"
            android:layout_height="74dp"
            android:textSize="35sp"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="25dp" />
    
        <RadioGroup
            android:id="@+id/RG"
            android:layout_width="264dp"
            android:layout_height="202dp"
            android:layout_marginTop="16dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            tools:ignore="MissingConstraints">
    
            <RadioButton
                android:id="@+id/RB1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton1"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="62dp"
                tools:layout_editor_absoluteY="87dp" />
    
            <RadioButton
                android:id="@+id/RB2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton2"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="62dp"
                tools:layout_editor_absoluteY="143dp" />
    
            <RadioButton
                android:id="@+id/RB3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton3"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="62dp"
                tools:layout_editor_absoluteY="214dp" />
    
            <RadioButton
                android:id="@+id/RB4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton4"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="62dp"
                tools:layout_editor_absoluteY="278dp" />
    
            <RadioButton
                android:id="@+id/RB5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton5"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="62dp"
                tools:layout_editor_absoluteY="356dp" />
    
            <RadioButton
                android:id="@+id/RB6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radiobutton6"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="61dp"
                tools:layout_editor_absoluteY="429dp" />
        </RadioGroup>
    
    
    </android.support.constraint.ConstraintLayout>
    

     MainActivity.java

    package com.example.aimee.radiogrouptest;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        TextView textview;
        RadioGroup RG;
        RadioButton RB1;
        RadioButton RB2;
        RadioButton RB3;
        RadioButton RB4;
        RadioButton RB5;
        RadioButton RB6;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            textview=findViewById(R.id.textView);
            RG=findViewById(R.id.RG);
            RB1=findViewById(R.id.RB1);
            RB2=findViewById(R.id.RB2);
            RB3=findViewById(R.id.RB3);
            RB4=findViewById(R.id.RB4);
            RB5=findViewById(R.id.RB5);
            RB6=findViewById(R.id.RB6);
    
            RG.setOnCheckedChangeListener(ChangeRadioGroup);
        }
        private RadioGroup.OnCheckedChangeListener ChangeRadioGroup=new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==RB1.getId()&&RB1.isChecked()){
                    textview.setText(RB1.getText());
                    Toast.makeText(MainActivity.this,RB1.getText()+"被选择",Toast.LENGTH_SHORT).show();
                }
                else if(checkedId==RB2.getId()&&RB2.isChecked()){
                    textview.setText(RB2.getText());
                    Toast.makeText(MainActivity.this,RB2.getText()+"被选择",Toast.LENGTH_SHORT).show();
    
                }
                else if(checkedId==RB3.getId()&&RB3.isChecked()){
                    textview.setText(RB3.getText());
                    Toast.makeText(MainActivity.this,RB3.getText()+"被选择",Toast.LENGTH_SHORT).show();
    
                }
                else if(checkedId==RB4.getId()&&RB4.isChecked()){
                    textview.setText(RB4.getText());
                    Toast.makeText(MainActivity.this,RB4.getText()+"被选择",Toast.LENGTH_SHORT).show();
    
                }
                else if(checkedId==RB5.getId()&&RB5.isChecked()){
                    textview.setText(RB5.getText());
                    Toast.makeText(MainActivity.this,RB5.getText()+"被选择",Toast.LENGTH_SHORT).show();
    
                }
                else if(checkedId==RB6.getId()&&RB6.isChecked()){
                    textview.setText(RB6.getText());
                    Toast.makeText(MainActivity.this,RB6.getText()+"被选择",Toast.LENGTH_SHORT).show();
    
                }
            }
        };
    }
    

     /res/values/string.xml

    <resources>
        <string name="app_name">RadioGroupTest</string>
        <string name="radiobutton1">Android</string>
        <string name="radiobutton2">Sysbian</string>
        <string name="radiobutton3">WinCE</string>
        <string name="radiobutton4">PalmOS</string>
        <string name="radiobutton5">Linux</string>
        <string name="radiobutton6">iphoneOS</string>
    </resources>
    
  • 相关阅读:
    tornado+websocket+mongodb实现在线视屏文字聊天
    mongoexport 导出需要授权数据库中的集合 报错 Authentication failed.
    nginx日志中添加请求的response日志
    SSE(Server-sent events)技术在web端消息推送和实时聊天中的使用
    RESTful接口设计原则和优点
    一次请求中,经过 nginx+uWSGI+flask应用程序搭建服务的执行过程
    项目中记录影响性能的缓慢数据库查询
    macos Item2 添加 Shell Integration (ftp传输)
    windows安装 阿里云的Fun工具
    windows10安装docker[含百度网盘docker安装包]
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/9854477.html
Copyright © 2011-2022 走看看