Android——使用多状态按钮ToggleButton(自己动手 丰衣足食)
2014-10-29 22:31
369 查看
最近在看慕课网的Android教程,其中的一节主要介绍开关按钮ToggleButton的 有一个小例子,控制灯泡的开关,下边是代码,(虽然很简单,但还是困难重重,先是clean clean 再clean把R文件都弄没啦,导入照片的时候自己不懂在每个文件夹下都导入了照片,以为分辨率不同的照片系统会自己选择,没想到只要一个就行)
布局文件layout:
activity:
再附上自己P稍微改变的两张照片,和运行后的截图
![](http://img.blog.csdn.net/20141029223631592)
![](http://img.blog.csdn.net/20141029223648143)
![](http://img.blog.csdn.net/20141029225142578?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTg3MDU0Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
布局文件layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ToggleButton android:id="@+id/toggleButton1" android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="false" android:textOn="开" android:textOff="关" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/off" /> </LinearLayout>
activity:
package com.oct; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.ToggleButton; public class MainActivity extends Activity implements OnCheckedChangeListener{ private ToggleButton tb; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.line); tb=(ToggleButton) findViewById(R.id.toggleButton1); img=(ImageView) findViewById(R.id.imageView1); tb.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //更改tb的状态,该参数代表当前Togglebutton是否被选中 tb.setChecked(isChecked); img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);//为img更换背景图片 } }
再附上自己P稍微改变的两张照片,和运行后的截图
相关文章推荐
- Android控件ToggleButton多状态按钮使用详解
- Android——Button的监听事件(自己动手 丰衣足食)
- 状态开关按钮ToggleButton的简单使用:实现动态控制布局
- Android——滚动视图(ScrollView)图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟
- ToggleButton(状态开关按钮)及Swich(开关)使用详解
- Android入门(5)——多状态按钮ToggleButton、多选按钮CheckBox、RadioGroup与RadioButtont按钮
- Android控件——多状态按钮ToggleButton
- Android开关按钮ToggleButton的使用,可以实现密码框和文本框的切换
- 状态开关按钮(ToggleButton)及按钮(Swich)的使用
- Android——图片视图(ImageView)、状态开关按钮(ToggleButton)、时钟、图片透明度、滚动和时间选择器
- Android界面UI开发中按钮Button的使用
- 在Android使用XML文件控制按钮文字在各种状态下的颜色
- Android Button 按钮 设置 各种状态 图片 颜色
- Android基础入门教程——2.3.6 开关按钮ToggleButton和开关Switch
- Android 编程笔记 select的使用,Button的三种状态(未点击,点击,选中保持状态)
- Android自学笔记之ToggleButton(开关按钮)的功能、特殊属性、用法
- android ToggleButton的使用
- 【Cocos2d-x】CCControlButton状态按钮的使用
- Android - Custom - SlidingToggleButton(滑动开关按钮)
- Android ToggleButton Example--开关按钮