版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wpwbb510582246/article/details/52592458
一、ToggleButton常用的XML属性
属性名称 |
描述 |
android:disabledAlpha |
设置按钮在禁用时透明度。 |
android:textOff |
未选中时按钮的文本 |
android:textOn |
选中时按钮的文本 |
二、用ToggleButton制作的一个控制电灯开关的程序
1、效果
2、源代码
activity_main.xml
<LinearLayout 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:background="#000000"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgBulb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bulb_off" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ToggleButton
android:id="@+id/tbBulb"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textOff="开灯"
android:textOn="关灯" />
<RadioGroup
android:id="@+id/rgChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbTurnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="关灯"
android:textColor="#ffffff" />
<RadioButton
android:id="@+id/rbTurnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开灯"
android:textColor="#ffffff" />
</RadioGroup>
<CheckBox
android:id="@+id/cbTurnOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开灯"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
bulb_off.png
bulb_on.png
MainActivity.java
- package com.weipeng.android.mytogglebutton;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.ImageView;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.ToggleButton;
- public class MainActivity extends Activity {
- private ImageView imgBulb;
- private RadioGroup rgChoice;
- private RadioButton rbTurnOn;
- private RadioButton rbTurnOff;
- private CheckBox cbTurnOn;
- private ToggleButton tbBulb;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imgBulb=(ImageView) findViewById(R.id.imgBulb);
- rgChoice=(RadioGroup) findViewById(R.id.rgChoice);
- rbTurnOn=(RadioButton) findViewById(R.id.rbTurnOn);
- rbTurnOff=(RadioButton) findViewById(R.id.rbTurnOff);
- cbTurnOn=(CheckBox) findViewById(R.id.cbTurnOn);
- tbBulb=(ToggleButton) findViewById(R.id.tbBulb);
- System.out.println("已执行:findViewById");
- tbBulb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- System.out.println("准备执行:onCheckedChanged");
- if(isChecked==true){
- rbTurnOn.setChecked(true);
- cbTurnOn.setChecked(true);
- cbTurnOn.setText("关灯");
- imgBulb.setImageResource(R.drawable.bulb_on);
- System.out.println("已执行:isChecked=true的操作");
- }
- else {
- rbTurnOff.setChecked(true);
- cbTurnOn.setChecked(false);
- cbTurnOn.setText("开灯");
- imgBulb.setImageResource(R.drawable.bulb_off);
- System.out.println("已执行:isChecked=false的操作");
- }
- }
- });
- rbTurnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked==true){
- tbBulb.setChecked(false);
- cbTurnOn.setChecked(false);
- cbTurnOn.setText("开灯");
- imgBulb.setImageResource(R.drawable.bulb_off);
- System.out.println("已执行:isChecked=true的操作");
- }
- else {
- tbBulb.setChecked(true);
- cbTurnOn.setChecked(true);
- cbTurnOn.setText("关灯");
- imgBulb.setImageResource(R.drawable.bulb_on);
- System.out.println("已执行:isChecked=false的操作");
- }
- }
- });
- rbTurnOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked==true){
- tbBulb.setChecked(true);
- cbTurnOn.setChecked(true);
- cbTurnOn.setText("关灯");
- imgBulb.setImageResource(R.drawable.bulb_on);
- System.out.println("已执行:isChecked=true的操作");
- }
- else {
- tbBulb.setChecked(false);
- cbTurnOn.setChecked(false);
- cbTurnOn.setText("开灯");
- imgBulb.setImageResource(R.drawable.bulb_off);
- System.out.println("已执行:isChecked=false的操作");
- }
- }
- });
- cbTurnOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked==true){
- tbBulb.setChecked(true);
- rbTurnOn.setChecked(true);
- cbTurnOn.setText("关灯");
- imgBulb.setImageResource(R.drawable.bulb_on);
- System.out.println("已执行:isChecked=true的操作");
- }
- else {
- tbBulb.setChecked(false);
- rbTurnOff.setChecked(true);
- cbTurnOn.setText("开灯");
- imgBulb.setImageResource(R.drawable.bulb_off);
- System.out.println("已执行:isChecked=false的操作");
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
由于本人初写博客,写的不好的地方还请大家能批评指正,希望能和大家相互学习、相互交流、共同成长。