zoukankan      html  css  js  c++  java
  • OnChencedChang

    (一)

    1,布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="helloworld.com.inspur.app5.MainActivity">
    
        <TextView android:layout_width="wrap_content"
            android:id="@+id/tv"
            android:layout_height="wrap_content"
            android:text="选择正确答案:" />
        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rb1"
                android:text="2+1=2"/>
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rb2"
                android:text="2+1=3"/>
            <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/rb3"
            android:text="2+1=4"/>
            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rb4"
                android:text="2+1=1"/>
    
    
        </RadioGroup>
    
    </LinearLayout>

    注意事项:RadioButton需要包含在RadioGroup内部才能实现单选的效果

    2,逻辑代码的处理

    package helloworld.com.inspur.app5;
    
    import android.support.v4.widget.TextViewCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
        private TextView tv;
        private RadioGroup rg;
        private RadioButton rb1;
        private RadioButton rb2;
        private RadioButton rb4;
        private RadioButton rb3;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv=(TextView)findViewById(R.id.tv);
            rg=(RadioGroup)findViewById(R.id.rg);
            rb1=(RadioButton)findViewById(R.id.rb1);
            rb2=(RadioButton)findViewById(R.id.rb2);
            rb4=(RadioButton)findViewById(R.id.rb4);
            rb3=(RadioButton)findViewById(R.id.rb3);
            rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId)
                    {
                        case R.id.rb1:
                            Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
    
                            break;
                        case R.id.rb2:
                            Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
                            break;
                        case R.id.rb3:
                        Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
                        break;
                        case R.id.rb4:
                            Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
                            break;
    
    
                    }
                }
            });
    
    
    
        }
    }

    注意:

     Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
    在后面加上.show()才会显示。

     (二)if判断id是否相同

    if(R.id.rb2==checkedId)
                    {
                        Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
                    }
                    else{
                        Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
                    }

    (三)if判断对象是否相同

    RadioButton r=(RadioButton)findViewById(checkedId);
                    if(rb2.equals(r))
                    {
                        Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
                    }
                    else{
                        Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
                    }
  • 相关阅读:
    【2017.12.02普及组模拟】送快递
    【NOIP2013模拟联考7】OSU
    顺序表元素位置倒置示例c++实现
    c++字符串排序
    JAVA实现四则运算的简单计算器
    JAVA图形小动画之简单行星运动
    JAVA多线程编程
    ege图形库之简单贪吃蛇(c++)
    ege图形库之动画排序
    c语言中一种典型的排列组合算法
  • 原文地址:https://www.cnblogs.com/excellencesy/p/9007849.html
Copyright © 2011-2022 走看看