zoukankan      html  css  js  c++  java
  • 选择改变事件OnCheckedChange

    1.效果图:选择正确的提示选对,选择错误提示选错

    2.activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6    android:orientation="vertical"
     7     tools:context="com.example.app5.MainActivity">
     8 
     9     <TextView
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content"
    12         android:text="请选择正确的题目" />
    13     <RadioGroup
    14         android:id="@+id/rg"
    15         android:layout_width="wrap_content"
    16         android:layout_height="wrap_content">
    17         <RadioButton
    18             android:id="@+id/rb_1"
    19             android:text="1+1=3"
    20             android:layout_width="wrap_content"
    21             android:layout_height="wrap_content" />
    22         <RadioButton
    23             android:id="@+id/rb_2"
    24             android:text="1+1=2"
    25             android:layout_width="wrap_content"
    26             android:layout_height="wrap_content" />
    27         <RadioButton
    28             android:id="@+id/rb_3"
    29             android:text="1+1=4"
    30             android:layout_width="wrap_content"
    31             android:layout_height="wrap_content" />
    32         <RadioButton
    33             android:id="@+id/rb_4"
    34             android:text="1+1=5"
    35             android:layout_width="wrap_content"
    36             android:layout_height="wrap_content" />
    37 
    38     </RadioGroup>
    39 </LinearLayout>

    2.MainActivity.java

     1 package com.example.app5;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.widget.RadioButton;
     6 import android.widget.RadioGroup;
     7 import android.widget.Toast;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10     private RadioGroup rg;
    11     private RadioButton rb_1,rb_2,rb_3,rb_4;
    12 
    13     @Override
    14     protected void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.activity_main);
    17         rg=(RadioGroup)findViewById(R.id.rg);
    18         rb_1 = (RadioButton) findViewById(R.id.rb_1);
    19         rb_2 = (RadioButton) findViewById(R.id.rb_2);
    20         rb_3= (RadioButton) findViewById(R.id.rb_3);
    21         rb_4 = (RadioButton) findViewById(R.id.rb_4);
    22 
    23         rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    24             @Override
    25             public void onCheckedChanged(RadioGroup group, int checkedId) {
    26 
    27                 //switch实现方式
    28                 /*switch (checkedId){
    29                     case R.id.rb_1:
    30                         Toast.makeText(MainActivity.this,"您选错了"+rb_1.isChecked(),Toast.LENGTH_SHORT).show();
    31                         break;
    32                     case R.id.rb_2:
    33                         Toast.makeText(MainActivity.this,"您选对了"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
    34                         break;
    35                     case R.id.rb_3:
    36                         Toast.makeText(MainActivity.this,"您选错了"+rb_3.isChecked(),Toast.LENGTH_SHORT).show();
    37                         break;
    38                     case R.id.rb_4:
    39                         Toast.makeText(MainActivity.this,"您选错了"+rb_4.isChecked(),Toast.LENGTH_SHORT).show();
    40                         break;*/
    41 
    42                  //if 判断实现方式
    43               /*  if(R.id.rb_2==checkedId){
    44                     Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
    45                 } else {
    46                     Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show();
    47                 }*/
    48 
    49                 //对象的实现方式
    50                 RadioButton r = (RadioButton) findViewById(checkedId);
    51                 if(r.equals(rb_2)){
    52                     Toast.makeText(MainActivity.this,"正确"+rb_2.isChecked(),Toast.LENGTH_SHORT).show();
    53                 }else{
    54                     Toast.makeText(MainActivity.this,"错误",Toast.LENGTH_SHORT).show();
    55                 }
    56             }
    57         });
    58 
    59 
    60     }
    61 }

     2.效果图:多选按钮,选择哪个之后提示选择了XX

    (1)activity_main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6    android:orientation="vertical"
     7     tools:context="com.example.app6.MainActivity">
     8 
     9     <TextView
    10         android:layout_width="wrap_content"
    11         android:layout_height="wrap_content"
    12         android:text="您的爱好是" />
    13     <CheckBox
    14         android:id="@+id/cb_1"
    15         android:text="sing"
    16         android:layout_width="wrap_content"
    17         android:layout_height="wrap_content" />
    18     <CheckBox
    19         android:id="@+id/cb_2"
    20         android:text="game"
    21         android:layout_width="wrap_content"
    22         android:layout_height="wrap_content" />
    23     <CheckBox
    24         android:id="@+id/cb_3"
    25         android:text="eat food"
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content" />
    28 
    29 </LinearLayout>
    View Code

    (2)MainActivity.java

     1 package com.example.app6;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.widget.CheckBox;
     6 import android.widget.CompoundButton;
     7 import android.widget.Toast;
     8 
     9 public class MainActivity extends AppCompatActivity {
    10 
    11     private CheckBox cb_1,cb_2,cb_3;
    12     @Override
    13     protected void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.activity_main);
    16         cb_1=(CheckBox)findViewById(R.id.cb_1);
    17         cb_2=(CheckBox)findViewById(R.id.cb_2);
    18         cb_3=(CheckBox)findViewById(R.id.cb_3);
    19 
    20         cb_1.setOnCheckedChangeListener(new myListener());
    21         cb_2.setOnCheckedChangeListener(new myListener());
    22         cb_3.setOnCheckedChangeListener(new myListener());
    23 
    24     }
    25    class myListener implements CompoundButton.OnCheckedChangeListener {
    26 
    27        @Override
    28        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    29 
    30            //switch实现
    31           /* switch (buttonView.getId()){
    32                case R.id.cb_1:
    33                    Toast.makeText(MainActivity.this,cb_1.getText().toString()+"  "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
    34                    break;
    35                case R.id.cb_2:
    36                    Toast.makeText(MainActivity.this,cb_2.getText().toString()+"  "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
    37                     break;
    38                case R.id.cb_3:
    39                    Toast.makeText(MainActivity.this,cb_3.getText().toString()+"  "+((CheckBox)buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
    40                     break;
    41            }*/
    42 
    43            //if实现
    44            if(isChecked){
    45                Toast.makeText(MainActivity.this,buttonView.getText().toString()+"  "+(buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
    46            } else{
    47                Toast.makeText(MainActivity.this,buttonView.getText().toString()+"  "+(buttonView).isChecked() ,Toast.LENGTH_SHORT).show();
    48 
    49            }
    50        }
    51    }
    52 }
    View Code

     3.效果图

     (1)activity_main.xml

    <?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:orientation="vertical"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.app7.MainActivity">
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="您的爱好是" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <CheckBox
                android:id="@+id/sing"
                android:text="sing"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <CheckBox
                android:id="@+id/game"
                android:text="game"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <CheckBox
                android:id="@+id/eat"
                android:text="eat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
        <Button
            android:id="@+id/bt"
            android:text="提交"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    (2)MainAcivity.xml

     1 package com.example.app7;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.Button;
     7 import android.widget.CheckBox;
     8 import android.widget.Toast;
     9 
    10 import java.util.ArrayList;
    11 import java.util.List;
    12 
    13 public class MainActivity extends AppCompatActivity {
    14     private Button bt;
    15     private CheckBox sing;
    16     private  CheckBox game;
    17     private  CheckBox eat;
    18     private List<CheckBox> list;
    19     private int count=0;
    20 
    21     @Override
    22     protected void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.activity_main);
    25 
    26         sing = (CheckBox) findViewById(R.id.sing);
    27         game = (CheckBox) findViewById(R.id.game);
    28         eat = (CheckBox) findViewById(R.id.eat);
    29         bt = (Button) findViewById(R.id.bt);
    30         list = new ArrayList<>();
    31         list.add(sing);
    32         list.add(game);
    33         list.add(eat);
    34 
    35         bt.setOnClickListener(new View.OnClickListener() {
    36             @Override
    37             public void onClick(View v) {
    38                 StringBuffer sb = new StringBuffer();;
    39                 for (CheckBox checkbox:list){
    40                     if (checkbox.isChecked()){
    41                         count++;
    42                         sb.append(checkbox.getText().toString()+"  ");
    43                     }
    44                 }
    45                 if(sb==null||"".equals(sb.toString())){
    46                     Toast.makeText(MainActivity.this,"至少选择一项",Toast.LENGTH_SHORT).show();
    47                 }else {
    48                     Toast.makeText(MainActivity.this,sb.toString()+",共"+count+"项",Toast.LENGTH_SHORT).show();
    49                     count=0;
    50                 }
    51             }
    52         });
    53     }
    54 }
  • 相关阅读:
    js高级教程阅读笔记 第一章-js的简介
    angular.element方法汇总
    AngularJS第六课(路由)
    AngularJS第五课(模块,动画,依赖注入)
    javascript基础整理(面试必备)
    Google工具page-speed使用教程(网站性能检测)
    常见前端面试题及答案
    css之布局那些事
    jquery之全屏滚动插件fullPage.js
    Git远程操作详解
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9007983.html
Copyright © 2011-2022 走看看