android开发中ToggleButton的使用方法详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wpwbb510582246/article/details/52592458

一、ToggleButton常用的XML属性

属性名称

描述

android:disabledAlpha

设置按钮在禁用时透明度。

 

android:textOff

未选中时按钮的文本

android:textOn

选中时按钮的文本

二、用ToggleButton制作的一个控制电灯开关的程序

1、效果

2、源代码

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#000000"
  6.     android:orientation="horizontal" >
  7.  
  8.     <ImageView
  9.         android:id="@+id/imgBulb"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:background="@drawable/bulb_off" />
  13.  
  14.     <LinearLayout
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:orientation="vertical" >
  18.  
  19.         <ToggleButton
  20.             android:id="@+id/tbBulb"
  21.             android:layout_width="135dp"
  22.             android:layout_height="wrap_content"
  23.             android:textColor="#ffffff"
  24.             android:textOff="开灯"
  25.             android:textOn="关灯" />
  26.  
  27.         <RadioGroup
  28.             android:id="@+id/rgChoice"
  29.             android:layout_width="wrap_content"
  30.             android:layout_height="wrap_content"
  31.             android:orientation="horizontal" >
  32.  
  33.             <RadioButton
  34.                 android:id="@+id/rbTurnOff"
  35.                 android:layout_width="wrap_content"
  36.                 android:layout_height="wrap_content"
  37.                 android:checked="true"
  38.                 android:text="关灯"
  39.                 android:textColor="#ffffff" />
  40.  
  41.  
  42.             <RadioButton
  43.                 android:id="@+id/rbTurnOn"
  44.                 android:layout_width="wrap_content"
  45.                 android:layout_height="wrap_content"
  46.                 android:text="开灯"
  47.                 android:textColor="#ffffff" />
  48.         </RadioGroup>
  49.  
  50.         <CheckBox
  51.             android:id="@+id/cbTurnOn"
  52.             android:layout_width="wrap_content"
  53.             android:layout_height="wrap_content"
  54.             android:text="开灯"
  55.             android:textColor="#ffffff" />
  56.     </LinearLayout>
  57.  
  58. </LinearLayout>

bulb_off.png

bulb_on.png

MainActivity.java

  1. package com.weipeng.android.mytogglebutton;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.widget.CheckBox;
  7. import android.widget.CompoundButton;
  8. import android.widget.ImageView;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.ToggleButton;
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15.  
  16.  
  17. private ImageView imgBulb;
  18.  
  19. private RadioGroup rgChoice;
  20. private RadioButton rbTurnOn;
  21. private RadioButton rbTurnOff;
  22.  
  23. private CheckBox cbTurnOn;
  24.  
  25. private ToggleButton tbBulb;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31.  
  32.  
  33. imgBulb=(ImageView) findViewById(R.id.imgBulb);
  34. rgChoice=(RadioGroup) findViewById(R.id.rgChoice);
  35. rbTurnOn=(RadioButton) findViewById(R.id.rbTurnOn);
  36. rbTurnOff=(RadioButton) findViewById(R.id.rbTurnOff);
  37. cbTurnOn=(CheckBox) findViewById(R.id.cbTurnOn);
  38. tbBulb=(ToggleButton) findViewById(R.id.tbBulb);
  39. System.out.println("已执行:findViewById");
  40.  
  41.  
  42. tbBulb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
  43.  
  44. @Override
  45. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  46. System.out.println("准备执行:onCheckedChanged");
  47. if(isChecked==true){
  48. rbTurnOn.setChecked(true);
  49. cbTurnOn.setChecked(true);
  50. cbTurnOn.setText("关灯");
  51. imgBulb.setImageResource(R.drawable.bulb_on);
  52. System.out.println("已执行:isChecked=true的操作");
  53. }
  54. else {
  55. rbTurnOff.setChecked(true);
  56. cbTurnOn.setChecked(false);
  57. cbTurnOn.setText("开灯");
  58. imgBulb.setImageResource(R.drawable.bulb_off);
  59. System.out.println("已执行:isChecked=false的操作");
  60. }
  61. }
  62. });
  63.  
  64. rbTurnOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  65. @Override
  66. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  67. if(isChecked==true){
  68. tbBulb.setChecked(false);
  69. cbTurnOn.setChecked(false);
  70. cbTurnOn.setText("开灯");
  71. imgBulb.setImageResource(R.drawable.bulb_off);
  72. System.out.println("已执行:isChecked=true的操作");
  73. }
  74. else {
  75. tbBulb.setChecked(true);
  76. cbTurnOn.setChecked(true);
  77. cbTurnOn.setText("关灯");
  78. imgBulb.setImageResource(R.drawable.bulb_on);
  79. System.out.println("已执行:isChecked=false的操作");
  80. }
  81. }
  82. });
  83. rbTurnOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  84. @Override
  85. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  86. if(isChecked==true){
  87. tbBulb.setChecked(true);
  88. cbTurnOn.setChecked(true);
  89. cbTurnOn.setText("关灯");
  90. imgBulb.setImageResource(R.drawable.bulb_on);
  91. System.out.println("已执行:isChecked=true的操作");
  92. }
  93. else {
  94. tbBulb.setChecked(false);
  95. cbTurnOn.setChecked(false);
  96. cbTurnOn.setText("开灯");
  97. imgBulb.setImageResource(R.drawable.bulb_off);
  98. System.out.println("已执行:isChecked=false的操作");
  99. }
  100. }
  101. });
  102. cbTurnOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  103. @Override
  104. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  105. if(isChecked==true){
  106. tbBulb.setChecked(true);
  107. rbTurnOn.setChecked(true);
  108. cbTurnOn.setText("关灯");
  109. imgBulb.setImageResource(R.drawable.bulb_on);
  110. System.out.println("已执行:isChecked=true的操作");
  111. }
  112. else {
  113. tbBulb.setChecked(false);
  114. rbTurnOff.setChecked(true);
  115. cbTurnOn.setText("开灯");
  116. imgBulb.setImageResource(R.drawable.bulb_off);
  117. System.out.println("已执行:isChecked=false的操作");
  118. }
  119. }
  120. });
  121.  
  122.  
  123. }
  124.  
  125. @Override
  126. public boolean onCreateOptionsMenu(Menu menu) {
  127. getMenuInflater().inflate(R.menu.main, menu);
  128. return true;
  129. }
  130.  
  131. }

 

由于本人初写博客,写的不好的地方还请大家能批评指正,希望能和大家相互学习、相互交流、共同成长。


猜你喜欢

转载自blog.csdn.net/wpwbb510582246/article/details/52592458