Android 实现开关灯效果_第1张图片

Android 实现开关灯效果_第2张图片



activity_main.xml


    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/linear_1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >



   

        android:id="@+id/p_w_picpath_1"

        android:layout_width="150dp"

        android:layout_height="150dp"

        android:contentDescription="@string/deng"

        android:src="@drawable/bulb_off"

        android:text="@string/hello_world" />


   

        android:id="@+id/linear_2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingLeft="10dip"

        android:paddingTop="10dip" >


       

            android:id="@+id/toggle"

            android:layout_width="140dip"

            android:layout_height="wrap_content"

            android:textOff="@string/off"

            android:textOn="@string/on" />


       

            android:id="@+id/radio_1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >


           

                android:id="@+id/button_off"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:checked="true"

                android:text="@string/off" />


           

                android:id="@+id/button_on"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/on" />

       


       

            android:id="@+id/check"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/on" />

   




string.xml


<?xml version="1.0" encoding="utf-8"?>


    ToggleButton

    Settings

    Hello world!

   

    开灯

    关灯





MainActivity.java



package com.malakana.togglebutton;


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;

import android.widget.ToggleButton;

import android.app.Activity;


public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


ToggleButton tb = (ToggleButton) findViewById(R.id.toggle);

tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(arg1);

}

});


CheckBox cb = (CheckBox) this.findViewById(R.id.check);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(arg1);

}


});

RadioButton rb = (RadioButton) findViewById(R.id.button_off);

rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(!arg1);

}


});

}


public void setBulbState(boolean state) {

// TODO Auto-generated method stub

ImageView iv = (ImageView) findViewById(R.id.p_w_picpath_1);

iv.setImageResource((state) ? R.drawable.bulb_on : R.drawable.bulb_off);


ToggleButton tb = (ToggleButton) findViewById(R.id.toggle);

tb.setChecked(state);


CheckBox cb = (CheckBox) findViewById(R.id.check);

//cb.setText((state) ? R.string.on : R.string.off);

cb.setChecked(state);


RadioButton rb = (RadioButton) findViewById(R.id.button_off);

rb.setChecked(!state);

rb = (RadioButton) findViewById(R.id.button_on);

rb.setChecked(state);


}


}


更多相关文章

  1. android 改变 listView gridView的默认点击效果
  2. Android Button的背景图片拉伸变形解决方法
  3. Android 拖拽效果实现代码分享 - ORIETech
  4. android图片转换代码
  5. Android 异步从网络上下载图片
  6. android设置背景图片透明
  7. Android上传下载文件(图片)
  8. 安卓隐藏标题栏状态栏 ,实现全屏效果
  9. android界面模糊到清晰,效果去除

随机推荐

  1. Hello Android - proguard混淆器使用(签名
  2. android 弹出日期滑动选择框,日期滚动滑动
  3. 手机应用开发 第二节:PhoneGap and Dojo M
  4. 开源项目之Android Afinal框架
  5. Android下修改hosts文件
  6. android开机自启动APP及使用adb命令测试
  7. 红帆移动OA Android版开放公测!
  8. Adobe AIR for Android 中的GPU 渲染 [译
  9. App与Js交互(二)Android
  10. Android ListView组件中androiddrawSelec