checkbox没有什么好说就这样
public class MainActivity extends Activity
{
private CheckBox cbJava;
private CheckBox cbCSharp;
private CheckBox cbAbap;
private CheckBox cbPhp;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cbJava=(CheckBox)findViewById(R.id.cbJava);
cbCSharp=(CheckBox)findViewById(R.id.cbCSharp);
cbAbap=(CheckBox)findViewById(R.id.cbAbap);
cbPhp=(CheckBox)findViewById(R.id.cbPhp);
cbJava.setOnCheckedChangeListener(listener);
cbCSharp.setOnCheckedChangeListener(listener);
cbAbap.setOnCheckedChangeListener(listener);
cbPhp.setOnCheckedChangeListener(listener);
}
private OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (buttonView.getId()==R.id.cbJava)
{
if (isChecked)
{
Toast.makeText(MainActivity.this, "你学过Java", Toast.LENGTH_LONG).show();
}
}
else if (buttonView.getId()==R.id.cbCSharp)
{
if (isChecked)
{
Toast.makeText(MainActivity.this, "你学过C#", Toast.LENGTH_LONG).show();
}
}
else if (buttonView.getId()==R.id.cbAbap)
{
if (isChecked)
{
Toast.makeText(MainActivity.this, "你学过Abap", Toast.LENGTH_LONG).show();
}
}
else if (buttonView.getId()==R.id.cbPhp)
{
if (isChecked)
{
Toast.makeText(MainActivity.this, "你学过Php", Toast.LENGTH_LONG).show();
}
}
}
};
}
raidoButton比较特殊要放到一个RadioGroup里面,然后实现RadioGroup中的setOnCheckedChangeListener方法,达到点击改变的radiobutton的值的效果:
public class MainActivity extends Activity
{
private RadioGroup rgGender;
private RadioGroup rgQuestion;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rgGender=(RadioGroup)findViewById(R.id.rgGender);
rgGender.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (checkedId == R.id.rbBoy)
{
Toast.makeText(MainActivity.this, "男孩", Toast.LENGTH_LONG).show();
}
else if (checkedId == R.id.rbGirl)
{
Toast.makeText(MainActivity.this, "女孩", Toast.LENGTH_LONG).show();
}
}
});
rgQuestion=(RadioGroup)findViewById(R.id.rgQuestion);
rgQuestion.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if (checkedId == R.id.rbYes)
{
Toast.makeText(MainActivity.this, "毕业了", Toast.LENGTH_LONG).show();
}
else if (checkedId == R.id.rbNo)
{
Toast.makeText(MainActivity.this, "没有毕业", Toast.LENGTH_LONG).show();
}
}
});
}
}
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你的性别"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/rbBoy"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="男孩"
android:textSize="18sp"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"/>
/>
<RadioButton
android:id="@+id/rbGirl"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="女孩"
android:textSize="18sp"
android:drawableTop="#006699"
/>
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FFFFFF"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="你毕业了吗"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
/>
<RadioGroup
android:id="@+id/rgQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/rbYes"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="毕业了"
android:textSize="18sp"
/>
<RadioButton
android:id="@+id/rbNo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="没有毕业"
android:textSize="18sp"
/>
</RadioGroup>
</LinearLayout>