Android UI控件三
来源:互联网 发布:99云呼软件 编辑:程序博客网 时间:2024/06/05 00:36
Android UI控件三
一、RadioButton选项按钮控件
选项按钮可以用于多选一的应用中,如果想在选中的某一个选项按钮后,其它的选项按钮都被设置为未选中的状态,需要将<RadioButton>添加到<RadioGroup>标签中。
1.单选按钮的使用
public class MainActivity extends Activity {private RadioGroup group;private Button btn;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);group = (RadioGroup) findViewById(R.id.radiogroup01);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(new OnClickListener() {public void onClick(View v) {int len = group.getChildCount();String msg = "";for (int i = 0; i < len; i++) {RadioButton radioButton = (RadioButton) group.getChildAt(i);if (radioButton.isChecked()) {msg = radioButton.getText().toString();break;}}Toast.makeText(MainActivity.this, msg, 1).show();}});}}
<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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别:" /> <RadioGroup android:id="@+id/radiogroup01" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <Button android:id="@+id/button01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择性别" /></LinearLayout>
二、ToggleButton开关状态按钮控件
1.ToggleButton按钮的使用
public class MainActivity extends Activity {private ToggleButton toggleButton;private LinearLayout linearLayout;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);toggleButton = (ToggleButton) findViewById(R.id.ToggleButton);linearLayout = (LinearLayout) findViewById(R.id.linearlayout01);toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (toggleButton.isChecked()) {linearLayout.setOrientation(1);} else {linearLayout.setOrientation(0);}}});}}
<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" android:orientation="vertical" > <ToggleButton android:id="@+id/ToggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOff="横向排列" android:textOn="纵向排列" /> <LinearLayout android:id="@+id/linearlayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3" /> </LinearLayout></LinearLayout>
三、CheckBox复选框控件
CheckBox默认的情况下是未选中的状态,如果想修改这个默认值的话,可以将<checkbox>的android:checked设置为true或者使用CheckBox.setChecked方法设置都可以实现复选的功能。
1.复选框控件的使用
public class MainActivity extends Activity implements OnClickListener {private List<CheckBox> CheckBoxs = new ArrayList<CheckBox>();private Button btn;// private CheckBox checkBox;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// setContentView(R.layout.main);String[] CheckBoxText = new String[] { "地球", "亚洲", "中国", "上海", "杨浦","浦东" };// 动态加载布局LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);// 给CheckBox赋值for (int i = 0; i < CheckBoxText.length; i++) {// 获得checkBox.xml对象CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);CheckBoxs.add(checkBox);CheckBoxs.get(i).setText(CheckBoxText[i]);linearLayout.addView(checkBox, i);}setContentView(linearLayout);btn = (Button) findViewById(R.id.button01);btn.setOnClickListener(this);}public void onClick(View v) {String msg = "";for (CheckBox checkBox : CheckBoxs) {if (checkBox.isChecked()) {msg += checkBox.getText().toString() + "\n";}}if (msg.equals("")) {msg = "您还没有选择!";}new AlertDialog.Builder(this).setMessage(msg).setPositiveButton("关闭", null).show();}}
checkbox.xml
<?xml version="1.0" encoding="utf-8"?><CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/checkbox01" android:layout_width="match_parent" android:layout_height="wrap_content" ></CheckBox>
main.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" android:orientation="vertical" > <Button android:id="@+id/button01" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确定" /></LinearLayout>
- Android UI控件三
- Android 学习(三)上: UI 控件
- Android 学习(三)下: UI 控件
- 【Android基础】(9)UI控件(三)
- UI基本三控件
- Android Material Design新UI控件使用大全 三
- Android Material Design新UI控件使用大全 三
- Android Material Design新UI控件使用大全 三
- UI控件使用案例<三>
- android UI控件学习
- Android UI控件一
- Android UI控件二
- Android UI控件四
- Android UI控件五
- Android UI控件再试
- Android ui控件详解
- Android控件:高仿微信主UI
- Android UI控件大全
- .net 多态初探
- phpstorm xdebug apache(调试)
- 软件工程
- java虚拟机的运行机理
- 正则表达式常用实例
- Android UI控件三
- QQ2013协议分析(一)准备工作
- Opencv通过摄像头画框
- python 重定向shell执行输出
- Myeclipse10下载,安装,破解,插件,优化介绍
- strcpy函数与memcpy函数(区别及实现)
- DB2 ONLINE UTILITY 监控
- Ubuntu下source insight安装
- struts中过滤器与拦截器的区别