在研发过程中,会经常遇到用户的性别选择,为此特地写了一篇,文章采用的是TableLayout结合TableRow实现的;


下面我们来看实现:

1.布局use_male_female.xml


<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/tab"    android:layout_width="match_parent"    android:layout_height="match_parent">                        android:layout_width="wrap_content"            android:layout_height="wrap_content"         android:layout_marginTop="8dp"            android:text="性别:"/>                    android:id="@+id/rg"            android:orientation="horizontal"            android:layout_gravity="center_horizontal">                            android:id="@+id/male"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="true"                android:text="" />                            android:id="@+id/femle"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:checked="true"                android:text=""/>                        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:visibility="gone"        android:id="@+id/show"/>

下面我们在来看看代码实现;


@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. Android中自定义ScrollView代码实例
  2. Android摄像头采集Demo
  3. Linux/Android启动之Machine-Init函数
  4. Android下载文件,如果文件夹下有同名文件,则重命名规则为a(2)、a(3
  5. Android(安卓)MediaCodec参数笔记
  6. Android(安卓)全局变量
  7. 【android】去掉标题栏
  8. Android实现自定义时钟控件
  9. [置顶] 自定义漂亮的Android(安卓)SeekBar样式

随机推荐

  1. Android寮€鍙戦」鐩疄璁璂ay_2
  2. CactiPhone: 移动设备上查看Cacti
  3. 【起航计划 016】2015 起航计划 Android(
  4. 如何使Android应用程序获取系统权限【转
  5. android之有返回结果跳转intent
  6. 通过终端命令生成并在手机上运行dex文件
  7. Robotium---环境搭建及入门示例
  8. Android学习笔记:Android基础知识总结
  9. android的技术层次
  10. Spark实例-每天每个搜索词用户访问