Android——单选多选按钮的使用详解

<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=“ r e l a t i v e P a c k a g e . {relativePackage}. 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多选” />

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=“ r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}” >

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“@string/hello_world” />

<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=“女”/>

<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=“没吃”/>

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

/*switch (checkedId) {

case R.id.radio1:

Toast.makeText(this, “男”, Toast.LENGTH_LONG).show();

break;

case R.id.radio2:

Toast.makeText(this, “女”, Toast.LENGTH_LONG).show();

break;

case R.id.radio3:

Toast.makeText(this, “吃了”, Toast.LENGTH_LONG).show();

break;

case R.id.radio4:

Toast.makeText(this, “没吃”, Toast.LENGTH_LONG).show();

break;

default:

break;

}*/

//找到点击的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=“ r e l a t i v e P a c k a g e . {relativePackage}. 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=“提交” />

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持)

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT大厂面试题(有解析)

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

src=“https://i-blog.csdnimg.cn/blog_migrate/937095e2196110686beac5aa0c40a09a.jpeg” />

文末

我总结了一些Android核心知识点,以及一些最新的大厂面试题、知识脑图和视频资料解析。

以后的路也希望我们能一起走下去。(谢谢大家一直以来的支持)

部分资料一览:

  • 330页PDF Android学习核心笔记(内含8大板块)

[外链图片转存中…(img-6aq8y7tt-1713221047637)]

[外链图片转存中…(img-ws8VTgj3-1713221047638)]

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

[外链图片转存中…(img-rgX2Zxm6-1713221047638)]

  • Android BAT大厂面试题(有解析)

[外链图片转存中…(img-pIKhu5W9-1713221047640)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值