码迷,mamicode.com
首页 > 移动开发 > 详细

android studio开发笔记四

时间:2016-09-03 13:32:31      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

checkBox:复选框
1.两种状态:选中和未选中
2.属性:
android:id android:layout_width: android:layout_height:
android:checked android:text

RadioGroup和RadioButton
1.RadioGroup:RadioButton的一个集合,提供多选一机制
2.属性:android:orientation="vertical"--垂直分布
"horizontal"--水平分布

activitymain:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.checkbox.MainActivity">

<CheckBox
android:id="@+id/checkBox1"
android:checked="false"
android:text="篮球"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<RadioGroup
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup1"
>
<RadioButton
android:id="@+id/radioButton1"
android:checked="false"
android:text="男"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radioButton2"
android:checked="false"
android:text="女"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>

MainActivity:
package com.example.administrator.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
private CheckBox checkBox;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
checkBox=(CheckBox)findViewById(R.id.checkBox1);
rg=(RadioGroup)findViewById(R.id.radioGroup1);
//设置checkBox的监听事件来对CheckBox是不是被选中
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//通过oncheckedchanged方法来监听当前的checkbox是否被 选中
Log.i("tag",isChecked+"");
if(isChecked){
//获取checkbox的文本内容
String text=checkBox.getText().toString();
Log.i("tag",text);
}
}
});
//实现RadioGroup监听事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radioButton1:
Log.i("tab","you are a boy!");
break;
case R.id.radioButton2:
Log.i("tag","you are a girl!");
break;
}
}
});
}
}

android studio开发笔记四

标签:

原文地址:http://www.cnblogs.com/xy95/p/5836691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!