android模拟点击开关机按钮,Android 实现开关灯效果

6c3e2a87e1e9964e42bb5356ffa2f40e.png

b2275283b4544d5f3b1d1396d60ac848.png

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

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);

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值