android 实现男女按钮选择
16lz
2021-01-25
在研发过程中,会经常遇到用户的性别选择,为此特地写了一篇,文章采用的是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();} } }); }}
效果图:
以上比较简单,在开发中可以根据自己的要求进行定义,在这里就不多做解释了;
希望有帮助;
谢谢!
更多相关文章
- Android中自定义ScrollView代码实例
- Android摄像头采集Demo
- Linux/Android启动之Machine-Init函数
- Android下载文件,如果文件夹下有同名文件,则重命名规则为a(2)、a(3
- Android(安卓)MediaCodec参数笔记
- Android(安卓)全局变量
- 【android】去掉标题栏
- Android实现自定义时钟控件
- [置顶] 自定义漂亮的Android(安卓)SeekBar样式