iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android单选多选按钮怎么使用
  • 377
分享到

Android单选多选按钮怎么使用

2023-06-30 14:06:15 377人浏览 八月长安
摘要

这篇文章主要介绍了Android单选多选按钮怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android单选多选按钮怎么使用文章都会有所收获,下面我们一起来看看吧。一、单选按钮单选按钮类:RadioBu

这篇文章主要介绍了Android单选多选按钮怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android单选多选按钮怎么使用文章都会有所收获,下面我们一起来看看吧。

一、单选按钮

单选按钮类:RadioButton

android:checked="true"设置默认选中

单选按钮控件通常与RadioGroup搭配使用。 

  • RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组。 

  • 同一按钮组内的单选按钮只能有一个被选中。

二、多选按钮

用法基本与Button相同

CheckBox对象.isChecked()方法可以用来判断复选按钮是否选中 

效果图(单选多选写在一个项目里边,用了一个页面跳转):

项目目录:

Android单选多选按钮怎么使用

Android单选多选按钮怎么使用

Android单选多选按钮怎么使用

多选按钮,两种形式

Android单选多选按钮怎么使用

Android单选多选按钮怎么使用

代码:

activity_main.xml

<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >     <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="RadioActivity单选" />     <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="CheckActivity多选" /> </LinearLayout>

MainActivity.java

package com.example.radioandcheckdemo; import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{     private Button button1;    private Button button2;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);        button1.setOnClickListener(this);        button2.setOnClickListener(this);            }     @Override    public void onClick(View v) {        Intent intent = new Intent();        switch (v.getId()) {        case R.id.button1:            //跳转页面            intent.setClass(MainActivity.this, RadioActivity.class);            startActivity(intent);            break;        case R.id.button2:            //跳转页面            intent.setClass(MainActivity.this, CheckActivity.class);            startActivity(intent);        default:            break;        }    }}

activity_radio.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:layout_margin="20sp"    tools:context="${relativePackage}.${activityClass}" >     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />     <!--         单选        android:checked="true"设置默认选中     -->    <RadioGroup        android:id="@+id/group1"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content" >                <RadioButton             android:id="@+id/radio1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="男"/>         <RadioButton              android:id="@+id/radio2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"/>            </RadioGroup>     <!-- 分界线 -->    <View        android:layout_width="match_parent"        android:layout_height="2sp"        android:background="@android:color/holo_blue_dark"        android:layout_marginTop="10sp"        android:layout_marginBottom="10sp" />        <TextView         android:id="@+id/text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="18sp"        android:text="你吃饭了吗?"/>     <RadioGroup        android:id="@+id/group2"        android:layout_width="match_parent"        android:layout_height="wrap_content" >                <RadioButton             android:id="@+id/radio3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="吃了"/>         <RadioButton             android:id="@+id/radio4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="没吃"/>            </RadioGroup> </LinearLayout>

RadioActivity.java

package com.example.radioandcheckdemo; import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast; public class RadioActivity extends Activity implements OnCheckedChangeListener {    private RadioGroup group1;    private RadioGroup group2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_radio);                group1 = (RadioGroup) findViewById(R.id.group1);         group2 = (RadioGroup) findViewById(R.id.group2);         group1.setOnCheckedChangeListener(this);        group2.setOnCheckedChangeListener(this);    }        @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {        //显示值的几种方法                //checkedId选中RadioButton的id                        //找到点击的RadioButton        //RadioButton radio = (RadioButton) findViewById(checkedId);        //取出RadioButton中的值        //String str = radio.getText().toString();        //弹框显示选中的值        //Toast.makeText(this, str, Toast.LENGTH_LONG).show();                //两组数据同时显示        //根据RadioGroup取出数据,没有选中返回-1        String str = "";        int buttonId = group1.getCheckedRadioButtonId();        if(buttonId != -1){            RadioButton radio = (RadioButton) findViewById(buttonId);            str = "你的性别是" + radio.getText().toString();                    }else{            str = "你没有选择性别";        }        buttonId = group2.getCheckedRadioButtonId();        if(buttonId != -1){            RadioButton radio = (RadioButton) findViewById(buttonId);            str += ",   你吃饭了吗?"+radio.getText().toString();        }        Toast.makeText(this, str, Toast.LENGTH_LONG).show();    }}

activity_check.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="${relativePackage}.${activityClass}" >     <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="选择所学课程:" />     <CheckBox        android:id="@+id/check1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="html" />    <CheckBox        android:id="@+id/check2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="C" />    <CheckBox        android:id="@+id/check3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="PHP" />        <CheckBox        android:id="@+id/check4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="java" />     <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提交" /> </LinearLayout>

CheckActivity.java

package com.example.radioandcheckdemo; import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast; public class CheckActivity extends Activity {        private CheckBox check1;    private CheckBox check2;    private CheckBox check3;    private CheckBox check4;    private Button button1;        private OnCheckedChangeListener listenter = new OnCheckedChangeListener() {                @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            //选中多选框            CheckBox check = (CheckBox)buttonView;            //取出当前勾选值            String str = check.getText().toString();            //判断是否勾选状态            if(isChecked){                str = "你学了"+str;            }else{                str = "你没学"+str;            }            Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();        }    };     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_check);                check1 = (CheckBox) findViewById(R.id.check1);        check2 = (CheckBox) findViewById(R.id.check2);        check3 = (CheckBox) findViewById(R.id.check3);        check4 = (CheckBox) findViewById(R.id.check4);        button1 = (Button) findViewById(R.id.button1);                //多选框点击事件                        //提交按钮点击事件        button1.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                String str = "我学过了";                boolean f = false;                if(check1.isChecked()){                    str += check1.getText()+",";                    f = true;                }                if(check2.isChecked()){                    str += check2.getText()+",";                    f = true;                }                if(check3.isChecked()){                    str += check3.getText()+",";                    f = true;                }                if(check4.isChecked()){                    str += check4.getText()+",";                    f = true;                }                if(f){                    str = str.substring(0, str.length()-1);                }                Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();            }        });    }}

关于“Android单选多选按钮怎么使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android单选多选按钮怎么使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。

--结束END--

本文标题: Android单选多选按钮怎么使用

本文链接: https://www.lsjlt.com/news/329583.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android单选多选按钮怎么使用
    这篇文章主要介绍了Android单选多选按钮怎么使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android单选多选按钮怎么使用文章都会有所收获,下面我们一起来看看吧。一、单选按钮单选按钮类:RadioBu...
    99+
    2023-06-30
  • Android怎么实现单选按钮
    这篇文章主要介绍了Android怎么实现单选按钮的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android怎么实现单选按钮文章都会有所收获,下面我们一起来看看吧。单选按钮在默认情况下,单选按钮显示为一个圆形图...
    99+
    2023-06-30
  • Android单选按钮RadioButton如何使用
    使用Android的RadioButton组件进行单选按钮的选择,可以按照以下步骤进行操作:1. 在XML布局文件中添加RadioB...
    99+
    2023-08-16
    Android RadioButton
  • Android实现单选按钮
    本文实例为大家分享了Android实现单选按钮的具体代码,供大家参考,具体内容如下 单选按钮 在默认情况下,单选按钮显示为一个圆形图标,可以在图标旁放一些说明文字。通常情况下Radi...
    99+
    2024-04-02
  • Android单选按钮RadioButton怎么实现
    这篇文章主要介绍Android单选按钮RadioButton怎么实现,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!单选按钮要在一组中选择一项,并且不能多选。同一组RadioButton要放在同一个RadioGroup...
    99+
    2023-06-15
  • Vue怎么实现多选和单选按钮
    这篇“Vue怎么实现多选和单选按钮”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue怎么实现多选和单选按钮”文章吧。多选按...
    99+
    2023-07-05
  • Android单选按钮RadioButton的使用详解
    RadioButton是一种用于在Android应用程序中提供单选选项的视图组件。它通常与RadioGroup组件一起使用,以便只能...
    99+
    2023-08-15
    Android
  • Android单选按钮RadioButton的使用方法
    单选按钮要在一组中选择一项,并且不能多选。 同一组RadioButton要放在同一个RadioGroup节点下。 RadioButton默认未选中,点击后选中但是再次点击不会取消选中...
    99+
    2024-04-02
  • Android studio实现单选按钮
    本文实例为大家分享了Android studio实现单选按钮的具体代码,供大家参考,具体内容如下 创建空activity编辑activity_main.xml文件 代码如下: <...
    99+
    2024-04-02
  • Android 之 RadioButton (单选按钮)& Checkbox (复选框)
    本节引言: 本节给大家带来的是Andoird基本UI控件中的RadioButton和Checkbox; 先说下本节要讲解的内容是:RadioButton和Checkbox的1.基本用法 2.事件处理; 3.自定义点击效果; 4.改变文字...
    99+
    2023-08-31
    android ui
  • Android复选框CheckBox与开关按钮Switch及单选按钮RadioButton使用示例详解
    目录前言 一、复选框CheckBox二、开关按钮Switch三、单选按钮RadioButton单选组的用法前言  CompoundButton在XML文件中主要...
    99+
    2024-04-02
  • jquery怎么设置单选按钮选中
    要设置单选按钮选中,可以使用`prop()`方法或者`attr()`方法。使用`prop()`方法:```javascript// ...
    99+
    2023-08-12
    jquery
  • HTML单选和多选按钮怎么给图片加样式
    本文小编为大家详细介绍“HTML单选和多选按钮怎么给图片加样式”,内容详细,步骤清晰,细节处理妥当,希望这篇“HTML单选和多选按钮怎么给图片加样式”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起...
    99+
    2024-04-02
  • html单选按钮怎么设置
    非常抱歉,由于您没有提供文章标题,我无法为您生成一篇高质量的文章。请您提供文章标题,我将尽快为您生成一篇优质的文章。...
    99+
    2024-05-16
  • CSS单选按钮怎么实现
    本文小编为大家详细介绍“CSS单选按钮怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“CSS单选按钮怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。单选按钮因为box-shadow会按比例缩放,因此...
    99+
    2023-07-04
  • JQuery操作单选按钮以及复选按钮示例
    单选按钮以及复选按钮在开发过程中会经常用到,下面我就来通过JQuery操作单选按钮和复选按钮: 单选按钮: 通过JQuery获取单选按钮对象我们总共有三种途径: ①ID:$("#ra...
    99+
    2022-11-15
    单选按钮 复选按钮
  • 怎么用angular实现多选按钮的全选与反选
    这篇文章主要介绍了怎么用angular实现多选按钮的全选与反选,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。下面用angular来实现这一功...
    99+
    2024-04-02
  • Android编程如何实现带有单选按钮和复选按钮的dialog功能
    这篇文章将为大家详细讲解有关Android编程如何实现带有单选按钮和复选按钮的dialog功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体如下:带有单选按钮的dialog:package ...
    99+
    2023-05-30
    android dialog
  • 在HTML中怎么设置单选按钮
    这篇文章主要介绍了在HTML中怎么设置单选按钮,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。在HTML中设置单选按钮的方法:首先在新建的静态页面插入文件;然后在【<di...
    99+
    2023-06-06
  • Dreamweaver单选按钮如何出一道单选题?
    这篇文章给大家分享的是有关Dreamweaver单选按钮如何出一道单选题的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Dreamweaver中想要出一道单选题,该怎么出呢?下面我们就来看看dw单选题的制作方法。我...
    99+
    2023-06-08
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作