1.RadioButton
RadioButton被称作为单选框,通常都是以组的形式出现,可以在一组控件中选择一个。
RadioButton的使用首先需要加入<RadioGroup/>,在这个组中,我们进行单选按钮的声明。
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="51dp"
android:layout_y="182dp" > <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="172dp"
android:layout_y="181dp"
android:text="关灯" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="201dp"
android:text="开灯" />
</RadioGroup>
RadioButton
这里我们定义了两个RadioButton按钮,用来控制图片的切换,我们需要为RadioButton添加监听事件
Button myButton;
ImageButton myImg;
TextView textView;
ToggleButton myToggle;
ImageView img;
CheckBox myCheck; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton=(Button)findViewById(R.id.button1);
textView=(TextView)findViewById(R.id.text1);
myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
myCheck=(CheckBox)findViewById(R.id.checkBox1);
RadioButton radio=(RadioButton)findViewById(R.id.radioButton2);
RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}
});
radio.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}
}); }
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); RadioButton radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1=(RadioButton)findViewById(R.id.radioButton1);
radio1.setChecked(isChecked);
radio1=(RadioButton)findViewById(R.id.radioButton2);
radio1.setChecked(!isChecked); }
RadioButton监听
这里我们通过findViewById()来获取控件,并实现了控件的监听 setonCheckedChangeListener;
2.CheckBox
CheckBox控件被称为复选框,我们通过判断控件的选中状态,控制图片的切换。在资源文件中添加两个String对象,分别对应checkbox的选中状态,checkbox可以在不同的状态显示不同的Text。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myCheck=(CheckBox)findViewById(R.id.checkBox1); myCheck.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
}});
}
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); myCheck=(CheckBox)findViewById(R.id.checkBox1);
myCheck.setText((isChecked)?R.string.offn:R.string.onn);
myCheck.setChecked(isChecked);
}
3.ToogleButton
ToogleButton俗称开关控件,可以分别设置它的EditTextOn和EditTextOff两个状态下的文字,对于该控件也需要添加监听的事件,获取控件的状态。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); myToggle=(ToggleButton)findViewById(R.id.toggleButton1); myToggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO 自动生成的方法存根
setBulbState(isChecked);
} }
private void setBulbState(boolean isChecked) {
// TODO 自动生成的方法存根
img=(ImageView)findViewById(R.id.imageView1); img.setImageResource((isChecked)?R.drawable.bulbon:R.drawable.buldoff); myToggle=(ToggleButton)findViewById(R.id.toggleButton1);
myToggle.setChecked(isChecked);
}
ToogleButton控件
4.Xml文件
Xml前台设置文件如下:
<AbsoluteLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="180dp"
android:layout_height="64dp"
android:layout_x="45dp"
android:layout_y="269dp"
android:background="@drawable/btn01"
android:text="Button" /> <ImageButton
android:id="@+id/imageButton1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_x="139dp"
android:layout_y="399dp"
android:background="@drawable/easyicon_net_24"
android:src="@drawable/imgbutton" /> <ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="145dp"
android:layout_y="211dp"
android:text="ToggleButton"
android:textOff="开灯"
android:textOn="关灯" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="77dp"
android:layout_height="77dp"
android:layout_x="30dp"
android:layout_y="84dp"
android:src="@drawable/buldoff" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="164dp"
android:layout_y="115dp"
android:text="@string/onn" /> <RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="51dp"
android:layout_y="182dp" > <RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="172dp"
android:layout_y="181dp"
android:text="关灯" /> <RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="36dp"
android:layout_y="201dp"
android:text="开灯" />
</RadioGroup> </AbsoluteLayout>
Xml文件