本文主要是介绍Android 之 电灯泡开关效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?xml version="1.0" encoding="utf-8"?>- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- > <!-- 声明一个线性布局 -->
- <ImageView
- android:id="@+id/ImageView01"
- android:src="@drawable/bulb_off"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal">
- </ImageView> <!-- 声明一个ImageView控件 -->
- <RadioGroup
- android:id="@+id/RadioGroup01"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"> <!-- 声明一个RadioGroup控件 -->
- <RadioButton
- android:text="@string/off"
- android:id="@+id/off"
- android:checked="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </RadioButton> <!-- 声明一个RadioButton控件 -->
- <RadioButton
- android:text="@string/on"
- android:id="@+id/on"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </RadioButton> <!-- 声明一个RadioButton控件 -->
- </RadioGroup>
- <CheckBox
- android:text="@string/on"
- android:id="@+id/CheckBox01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal">
- </CheckBox> <!-- 声明一个CheckBox控件 -->
- </LinearLayout>
- package com.ethan;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.ImageView;
- import android.widget.RadioButton;
- public class BulbActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- CheckBox cb=(CheckBox)this.findViewById(R.id.CheckBox01);
- cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){//为CheckBox添加监听器及开关灯业务代码
- @Override
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- setBulbState(isChecked);
- }
- });
- RadioButton rb=(RadioButton)findViewById(R.id.off);
- rb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ //为RadioButton添加监听器及开关灯业务代码
- @Override
- public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
- setBulbState(!isChecked);
- }
- });
- }
- //方法:设置程序状态的
- public void setBulbState(boolean state){
- //设置图片状态
- ImageView iv=(ImageView)findViewById(R.id.ImageView01);
- iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off);
- CheckBox cb=(CheckBox)this.findViewById(R.id.CheckBox01);
- cb.setText((state)?R.string.off:R.string.on);
- cb.setChecked(state); //设置复选框文字状态
- RadioButton rb=(RadioButton)findViewById(R.id.off);
- rb.setChecked(!state);
- rb=(RadioButton)findViewById(R.id.on);
- rb.setChecked(state); //设置单选按钮状态
- }
- }
源代码:http://115.com/file/dpfwj2k9
这篇关于Android 之 电灯泡开关效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!