复选框
xml
------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<CheckBox android:id="@+id/CheckBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox1"
></CheckBox>
<CheckBox android:id="@+id/CheckBox2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox2"
></CheckBox>
<CheckBox
android:id="@+id/CheckBox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox3"
/>
</LinearLayout>
java
--------------------
package com.example.huanglifeng.menudemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity{
CheckBox m_CheckBox1;
CheckBox m_CheckBox2;
CheckBox m_CheckBox3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_CheckBox1=(CheckBox)findViewById(R.id.CheckBox1);
m_CheckBox2=(CheckBox)findViewById(R.id.CheckBox2);
m_CheckBox3=(CheckBox)findViewById(R.id.CheckBox3);
final CheckBox.OnCheckedChangeListener btnListener=new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
int num=0;
String strTmp="";
if(m_CheckBox1.isChecked())
{
num++;
strTmp+=m_CheckBox1.getText()+"\n";
}
if(m_CheckBox2.isChecked())
{
num++;
strTmp+=m_CheckBox2.getText()+"\n";
}
if(m_CheckBox3.isChecked())
{
num++;
strTmp+=m_CheckBox3.getText()+"\n";
}
strTmp="你选择了"+num+"项,他们分别是:"+"\n"+strTmp;
DisplayToast(strTmp);
}
};
m_CheckBox1.setOnCheckedChangeListener(btnListener);
m_CheckBox2.setOnCheckedChangeListener(btnListener);
m_CheckBox3.setOnCheckedChangeListener(btnListener);
}
public void DisplayToast(String str)
{
Toast toast=Toast.makeText(this,str,Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP,0,220);
toast.show();
}
}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer