无涯教程-Android - RadioGroup函数

RadioGroup类用于单选按钮集。

如果我们选中属于某个单选按钮组的一个单选按钮,它将自动取消选中同一组中以前选中的任何单选按钮。

RadioGroup属性

以下是与RadioGroup控制相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。

属性 说明
android:checkedButton 这是默认情况下应在此单选组中选中的子单选按钮的ID。

继承自 android.view.View 类-

Sr.No. Attribute & 描述
1 android:background 这是一个可绘制的背景。
2 android:content描述 这定义了简短描述视图内容的文本。
3 android:id 这提供了该视图的标识符名称
4 android:onClick 这是单击该视图时在该视图中要调用的方法的名称。
5 android:visibility 这控制了视图的初始可见性。

示例

本示例将带您通过简单的步骤,展示如何使用Linear Layout和RadioGroup创建自己的Android应用程序。

以下是修改后的主要Activity文件 src/MainActivity.java 的内容。该文件可以包括每个基本生命周期方法。

复制代码
package com.example.saira_000.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);


```xml
  btnDisplay</span><span class="pun">=(</span><span class="typ">Button</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">button</span><span class="pun">);</span><span class="pln">
  
  btnDisplay</span><span class="pun">.</span><span class="pln">setOnClickListener</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">View</span><span class="pun">.</span><span class="typ">OnClickListener</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
     </span><span class="lit">@Override</span><span class="pln">
     </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onClick</span><span class="pun">(</span><span class="typ">View</span><span class="pln"> v</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        </span><span class="kwd">int</span><span class="pln"> selectedId</span><span class="pun">=</span><span class="pln">radioSexGroup</span><span class="pun">.</span><span class="pln">getCheckedRadioButtonId</span><span class="pun">();</span><span class="pln">
        radioSexButton</span><span class="pun">=(</span><span class="typ">RadioButton</span><span class="pun">)</span><span class="pln">findViewById</span><span class="pun">(</span><span class="pln">selectedId</span><span class="pun">);</span><span class="pln">
        </span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">makeText</span><span class="pun">(</span><span class="typ">MainActivity</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">,</span><span class="pln">radioSexButton</span><span class="pun">.</span><span class="pln">getText</span><span class="pun">(),</span><span class="typ">Toast</span><span class="pun">.</span><span class="pln">LENGTH_SHORT</span><span class="pun">).</span><span class="pln">show</span><span class="pun">();</span><span class="pln">
     </span><span class="pun">}</span><span class="pln">
  </span><span class="pun">});</span><span class="pln">

```



}
}

以下是 res/layout/activity_main.xml 文件的内容-

复制代码
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp" />


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Learnfk"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:textSize="35dp"
android:textColor="@android:color/holo_green_dark" />


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/abc"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView" />


<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_below="@+id/imageView"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="@+id/radioGroup"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_alignRight="@+id/textView3"
android:layout_alignEnd="@+id/textView3">


```xml
  </span><span class="tag">&lt;RadioButton</span><span class="pln">
     </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">
     </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"55dp"</span><span class="pln">
     </span><span class="atn">android:text</span><span class="pun">=</span><span class="atv">"Male"</span><span class="pln">
     </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/radioButton"</span><span class="pln">
     </span><span class="atn">android:layout_gravity</span><span class="pun">=</span><span class="atv">"center_horizontal"</span><span class="pln">
     </span><span class="atn">android:checked</span><span class="pun">=</span><span class="atv">"false"</span><span class="pln">
     </span><span class="atn">android:textSize</span><span class="pun">=</span><span class="atv">"25dp"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">

  </span><span class="tag">&lt;RadioButton</span><span class="pln">
     </span><span class="atn">android:layout_width</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">
     </span><span class="atn">android:layout_height</span><span class="pun">=</span><span class="atv">"wrap_content"</span><span class="pln">
     </span><span class="atn">android:text</span><span class="pun">=</span><span class="atv">"Female"</span><span class="pln">
     </span><span class="atn">android:id</span><span class="pun">=</span><span class="atv">"@+id/radioButton2"</span><span class="pln">
     </span><span class="atn">android:layout_gravity</span><span class="pun">=</span><span class="atv">"center_horizontal"</span><span class="pln">
     </span><span class="atn">android:checked</span><span class="pun">=</span><span class="atv">"false"</span><span class="pln">
     </span><span class="atn">android:textSize</span><span class="pun">=</span><span class="atv">"25dp"</span><span class="pln">
     </span><span class="atn">android:layout_weight</span><span class="pun">=</span><span class="atv">"0.13"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">

```



</RadioGroup>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="    Are you?"
android:id="@+id/textView3"
android:textSize="35dp"
android:layout_below="@+id/imageView"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true" />


</RelativeLayout>

以下是 res/values/strings.xml 的内容,以定义这些新常量-

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Applicaiton</string>
   <string name="example_radiogroup">Example showing RadioGroup</string>
</resources>

以下是 AndroidManifest.xml 的默认内容-

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.saira_000.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >


```xml
  </span><span class="tag">&lt;activity</span><span class="pln">
     </span><span class="atn">android:name</span><span class="pun">=</span><span class="atv">"com.example.My Application.MainActivity"</span><span class="pln">
     </span><span class="atn">android:label</span><span class="pun">=</span><span class="atv">"@string/app_name"</span><span class="pln"> </span><span class="tag">&gt;</span><span class="pln">
  
     </span><span class="tag">&lt;intent-filter&gt;</span><span class="pln">
        </span><span class="tag">&lt;action</span><span class="pln"> </span><span class="atn">android:name</span><span class="pun">=</span><span class="atv">"android.intent.action.MAIN"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
        </span><span class="tag">&lt;category</span><span class="pln"> </span><span class="atn">android:name</span><span class="pun">=</span><span class="atv">"android.intent.category.LAUNCHER"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
     </span><span class="tag">&lt;/intent-filter&gt;</span><span class="pln">
  
  </span><span class="tag">&lt;/activity&gt;</span><span class="pln">
  

```



</application>
</manifest>

参考链接

www.learnfk.com/android/and...

相关推荐
开心工作室_kaic3 分钟前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿22 分钟前
webWorker基本用法
前端·javascript·vue.js
清灵xmf1 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据1 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161771 小时前
防抖函数--应用场景及示例
前端·javascript
334554322 小时前
element动态表头合并表格
开发语言·javascript·ecmascript
John.liu_Test2 小时前
js下载excel示例demo
前端·javascript·excel
PleaSure乐事2 小时前
【React.js】AntDesignPro左侧菜单栏栏目名称不显示的解决方案
前端·javascript·react.js·前端框架·webstorm·antdesignpro
哟哟耶耶2 小时前
js-将JavaScript对象或值转换为JSON字符串 JSON.stringify(this.SelectDataListCourse)
前端·javascript·json
理想不理想v2 小时前
vue种ref跟reactive的区别?
前端·javascript·vue.js·webpack·前端框架·node.js·ecmascript