发布于 2017-09-28 00:38:46 | 129 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧

在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧:


xml文件


<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" 
tools:context=".MainActivity" 
android:orientation="vertical"> 
<TextView 
android:id="@+id/txt" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="您的性别为"/> 
<RadioGroup 
android:id="@+id/sex" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
<RadioButton 
android:id="@+id/male" 
android:text="男"/> 
<RadioButton 
android:id="@+id/female" 
android:text="女"/> 
</RadioGroup> 
</LinearLayout> 

java文件


public class
MainActivity extends Activity { 
private TextView txt=null; 
private RadioGroup sex=null; 
private RadioButton male=null; 
private RadioButton female=null; 
@Override 
protected void
onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
this.txt=(TextView)
super.findViewById(R.id.txt); 
this.sex=(RadioGroup)
super.findViewById(R.id.sex); 
this.male=(RadioButton)
super.findViewById(R.id.male); 
this.female=(RadioButton)
super.findViewById(R.id.female); 
this.sex.setOnCheckedChangeListener(new
OnCheckedChangeListenerImp()); 
} private class
OnCheckedChangeListenerImp implements
OnCheckedChangeListener{ 
public void
onCheckedChanged(RadioGroup group, int checkedId)
{ String temp=null; 
if(MainActivity.this.male.getId()==checkedId){ 
temp="男"; 
} else if(MainActivity.this.female.getId()==checkedId){ 
temp="女"; 
} MainActivity.this.txt.setText("您的性别是"+temp); 
} }

以上所述是小编给大家介绍的Android程序开发中单选按钮(RadioGroup)的使用详解,希望对大家有所帮助!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务