android 实现男女按钮选择
来源:互联网 发布:中国网络发展史插画 编辑:程序博客网 时间:2024/05/04 09:38
在研发过程中,会经常遇到用户的性别选择,为此特地写了一篇,文章采用的是TableLayout结合TableRow实现的;
下面我们来看实现:
1.布局use_male_female.xml:
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="性别:"/> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/femle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="女"/> </RadioGroup> </TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:id="@+id/show"/></TableLayout>
下面我们在来看看代码实现;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.use_male_female); rg = (RadioGroup) findViewById(R.id.rg); show = (TextView) findViewById(R.id.show); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { tip = checkedId == R.id.male ? "男人" : "女人"; show.setVisibility(View.VISIBLE); show.setText(tip);//在这里同时可以根据小组定义数据传递到服务器; if(checkedId==R.id.male){ Toast.makeText(getApplicationContext(),"你选择了男",Toast.LENGTH_LONG).show();}else { Toast.makeText(getApplicationContext(),"你选择了女",Toast.LENGTH_LONG).show();} } }); }}
效果图:
以上比较简单,在开发中可以根据自己的要求进行定义,在这里就不多做解释了;
希望有帮助;
谢谢!
阅读全文
1 0
- android 实现男女按钮选择
- HTML中的单选按钮实现男女性别选择
- HTML中的单选按钮实现男女性别选择
- android初级学习之实现选择按钮
- [课堂笔记]选择类型控件 — 实现男女的选择
- CSS样式实现选择按钮
- android studio 如何实现RadioBotto和RadioGroup来实现单选按钮的选择
- android 点击按钮实现页面跳转并显示以选择信息
- android 实现透明按钮
- android 实现透明按钮
- Android 圆形按钮实现
- android 日期按钮实现
- Android 圆形按钮实现
- android实现移动按钮
- Android 圆形按钮实现
- MFC 实现浏览按钮选择文件
- 实现的radio图片选择按钮效果
- MFC中通过按钮实现选择图片
- Most Simple Usage Of Tensorboard
- 算法爱好者——背包问题升级版 ? 待解决
- 一台电脑同时连接多个git
- 蒜头君的排序(树状数组维护逆序对)
- Redis之Codis集群的搭建与使用-yellowcong
- android 实现男女按钮选择
- hdu 4803 贪心
- 实验:实现基于mysql的ftp虚拟用户
- Struts2+Spring+Hibernate的整合
- 网络工程师-海明码
- inotify通知机制
- 用malloc申请一个二维数组
- java字符串操作
- Java 并发 --- 阻塞队列之DelayQueue源码分析