- _nosay
Android学习笔记(三)
2017-12-27 14:25:03
复选框CheckBox概述
有两种状态
选中状态(true),未选中状态(false)
属性
android: id="@+id/checkbox" android: layout_width="wrap_content" android: layout_height="wrap_content" android: checked = "false" android: text="男"
布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:checked="false"
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="python" />
</LinearLayout>
java文件代码
package com.example.nosay.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
public class MainActivity extends AppCompatActivity{
private CheckBox checkBox;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1_main);
//初始化checkBox
checkBox = (CheckBox) findViewById(R.id.checkBox1);
//通过设置checkBox的监听事件,来判断当前的checkBox是否被选中
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//通过onCheckedChanged来监听当前的checkBox是否被选中
//如果b的值为true,说明是选中的,反之没选中
if(b)
{
//获得checkBox的文本内容
String s = checkBox.getText().toString();
Log.i("tag",s);
}else{
Log.i("tag","取消了选中");
}
}
});
}
}
RadioGroup和RadioButton
RadioGroup:
RadioButton的一个集合,提供多选一的机制
属性
android: orientation="vertical" //垂直排布 android: orientation="horizontal" //水平排布
决定当前RadioGroup中RadioButton以什么形式排列
布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:orientation="horizontal"
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_weight="1"
android:text="java" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="python" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="php" />
</RadioGroup>
</LinearLayout>
java代码如下
package com.example.nosay.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup radioGroup;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1_main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
//实现RadioGroup的监听事件
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i)
{
case R.id.radioButton1:
Log.i("tag","你选择的是java");
break;
case R.id.radioButton2:
Log.i("tag","你选择的是python");
break;
case R.id.radioButton3:
Log.i("tag","你选择的是php");
break;
}
}
}