广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程开发之RadioGroup用法实例
  • 591
分享到

Android编程开发之RadioGroup用法实例

radiogroupAndroid 2022-06-06 09:06:17 591人浏览 八月长安
摘要

本文实例讲述了Android编程开发之RadioGroup用法。分享给大家供大家参考,具体如下: RadioGroup 有时候比较有用.主要特征是给用户提供多选一机制。 Ma

本文实例讲述了Android编程开发之RadioGroup用法。分享给大家供大家参考,具体如下:

RadioGroup 有时候比较有用.主要特征是给用户提供多选一机制。

MainActivity.java


package com.example.lesson16_radio;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
 private RadioGroup group_temo;
 private RadioButton checkRadioButton;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  group_temo = (RadioGroup) findViewById(R.id.radioGroup1);
  // 改变默认选项
  group_temo.check(R.id.radio1);
  // 获取默认被被选中值
  checkRadioButton = (RadioButton) group_temo.findViewById(group_temo
    .getCheckedRadioButtonId());
  Toast.makeText(this, "默认的选项的值是:" + checkRadioButton.getText(),
    Toast.LENGTH_LONG).show();
  // 注册事件
  group_temo
    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {
      // 点击事件获取的选择对象
      checkRadioButton = (RadioButton) group_temo
        .findViewById(checkedId);
      Toast.makeText(getApplicationContext(),
        "获取的ID是" + checkRadioButton.getText(),
        Toast.LENGTH_LONG).show();
     }
    });
 }
}

布局文件


<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >
 <RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true" >
  <RadioButton
   android:id="@+id/radio0"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:checked="true"
   android:text="@string/text_java" />
  <RadioButton
   android:id="@+id/radio1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/text_net" />
  <RadioButton
   android:id="@+id/radio2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/text_PHP" />
 </RadioGroup>
</RelativeLayout>

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android控件系列之RadioButton与RadioGroup使用方法android RadioGroup的使用方法android自定义RadioGroup可以添加多种布局的实现方法Android程序开发中单选按钮(RadioGroup)的使用详解Android RadioGroup和RadioButton控件简单用法示例Android RadioGroup 设置某一个选中或者不可选中的方法让Android中RadioGroup不显示在输入法上面的办法Android ViewPager与radiogroup实现关联示例Android编程单选项框RadioGroup综合应用示例Android开发之RadioGroup的简单使用与监听示例


--结束END--

本文标题: Android编程开发之RadioGroup用法实例

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

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

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

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

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作