一、CheckBox与Toast的简单实用
1. CheckBox 复选操作。表示可以有多个选项提供勾选
CheckBox都有选中和未选中两种状态。
2. Toast:(吐司)是一种很便捷的提示方式,消耗资源很小。
- 提示用户一些简单的信息,不能与用户进行交互。
- 个人感觉跟layui的layer的某种提示风格很像,都是黑黑的,看着很舒服
- 显示的时长是有限的
案例:
Toast.makeText(this, result, 1).show();
步骤:
1.调用静态方法makeText,传入需要提示的信息以及需要显示的时间
2.在makeText方法中定义需要呈现的上下文,需要显示的提示内容以及时长
3.调用show方法进行显示。
3. 一个示例
activity_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"
tools:context="com.kjgym.checkbox.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择爱好:"
android:textSize="20sp" />
<CheckBox
android:id="@+id/playball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="踢球" />
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游泳" />
<CheckBox
android:id="@+id/sleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉" />
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跑步" />
<CheckBox
android:id="@+id/travel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旅游" />
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看选中" />
</LinearLayout>
MainActivity.java
package com.kjgym.checkbox;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Button check;
private CheckBox swim,playball,run,sleep,travel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 按钮初始化
check = (Button) findViewById(R.id.check);
swim = (CheckBox) findViewById(R.id.swim);
playball = (CheckBox) findViewById(R.id.playball);
run = (CheckBox) findViewById(R.id.run);
sleep = (CheckBox) findViewById(R.id.sleep);
travel = (CheckBox) findViewById(R.id.travel);
// 这里使用匿名内部类的形式进行事件监听
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
showChecked();
}
});
}
@SuppressLint("ShowToast") protected void showChecked() {
List<String> resultList = new ArrayList<String>();
if(swim.isChecked()){
resultList.add(swim.getText().toString());
}
if(playball.isChecked()){
resultList.add(playball.getText().toString());
}
if(run.isChecked()){
resultList.add(run.getText().toString());
}
if(sleep.isChecked()){
resultList.add(sleep.getText().toString());
}
if(travel.isChecked()){
resultList.add(travel.getText().toString());
}
if(resultList.size() != 0) {
StringBuilder sb = new StringBuilder("您选中的兴趣有:");
for(Iterator<String> it = resultList.iterator();it.hasNext();){
sb.append(it.next());
sb.append(",");
}
String result = sb.substring(0, sb.length()-1);
Toast.makeText(this, result, 1).show();
} else{
Toast.makeText(this, "您什么也没有选....", 1).show();
}
}
}
运行效果:
二、RadioButton与RadioGroup
一般情况下 我们是需要单选按钮之间存在互斥关系的。那么就需要将其放入一个RadioGroup中。
关于事件监听:
CompoundButton是RadioButton与CheckBox的父类,
而 OnCheckedChangeListener是定义在CompoundButton当中的监听,所以CheckBox与RadioButton都可以使用。
activity_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"
tools:context="com.kjgym.radiobutton.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别:" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/nan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
MainActivity.java
package com.kjgym.radiobutton;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private RadioButton nan,nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nan = (RadioButton) findViewById(R.id.nan);
nv = (RadioButton) findViewById(R.id.nv);
nan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "您选中了"+nan.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
nv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(getApplicationContext(), "您选中了"+nv.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
运行效果: