布局代码详情:(下配效果图)
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="22sp"
android:text="请选择喜欢的应用程序:" />
<CheckBox
android:id="@+id/cd_wx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
/>
<CheckBox
android:id="@+id/cd_wy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微云"
/>
<CheckBox
android:id="@+id/cd_wb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微博"
/>
</LinearLayout>
啊
第一种方法:
package com.ayxy.arrj_empathy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnCheckedChangeListener{
//接口 implements OnCheckedChangeListener 需要重写方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.导入布局
setContentView(R.layout.activity_main);
//2.创建对象
CheckBox cd_wx = (CheckBox) findViewById(R.id.cd_wx);//此处需要强制类型转换
CheckBox cd_wy = (CheckBox) findViewById(R.id.cd_wy);
CheckBox cd_wb = (CheckBox) findViewById(R.id.cd_wb);
//3.指挥对象做事情
cd_wx.setOnCheckedChangeListener(this);//CheckBox 使用的第一种办法
cd_wy.setOnCheckedChangeListener(this);
cd_wb.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 4.buttonView 事件源 isChecked 是否被选中
switch (buttonView.getId()) {
case R.id.cd_wx:
Toast.makeText(MainActivity.this, isChecked?"以选择:微信":"已取消", 0).show();
break;
case R.id.cd_wy:
Toast.makeText(MainActivity.this, isChecked?"以选择:微云":"已取消", 0).show();
break;
case R.id.cd_wb:
Toast.makeText(MainActivity.this, isChecked?"以选择:微博":"已取消", 0).show();
break;
default:
break;
}
}
}
第二种方法:
package com.ayxy.arrj_empathy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1.导入布局
setContentView(R.layout.activity_main);
//2.创建对象
CheckBox cd_wx = (CheckBox) findViewById(R.id.cd_wx);//此处需要强制类型转换
CheckBox cd_wy = (CheckBox) findViewById(R.id.cd_wy);
CheckBox cd_wb = (CheckBox) findViewById(R.id.cd_wb);
//3.指挥对象做事情
cd_wx.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//CheckBox 使用的第二种办法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView 事件源 isChecked 判断是否被选中
if(isChecked)
{Toast.makeText(MainActivity.this, "以选择微信", 0).show();}
else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
}
});
cd_wy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//CheckBox 使用的第二种办法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView 事件源 isChecked 判断是否被选中
if(isChecked)
{Toast.makeText(MainActivity.this, "以选择微云", 0).show();}
else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
}
});
cd_wb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//CheckBox 使用的第二种办法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView 事件源 isChecked 判断是否被选中
if(isChecked)
{Toast.makeText(MainActivity.this, "以选择微博", 0).show();}
else {Toast.makeText(MainActivity.this, "以取消", 0).show();}
}
});
}
}
奥的斯
电池的撒