学Android---ToggleButton
来源:互联网 发布:怎么看数据库密码 编辑:程序博客网 时间:2024/06/16 07:22
1、什么是ToggleButton:
Toggle,意思是切换,转换。ToggleButton自然就表示切换按钮了。它有两种状态,分别是选中和未选中,并且需要为不同的状态设置不同的显示文本。就像是手电筒的开关按钮一样,初始状态为关,按下按钮,手电打开,再按下又切换为关闭状态。
2、ToggleButton常用属性:
android:checked = “true” //表示启动程序时即为选中状态
android:textOff = “关” //状态为关时所显示的文字
android:textOn = “开” //状态开时所显示的文字
3、ToggleButton的实现(滑动到最后可看到效果):
(1)首先在布局中添加ToggleButton控件,为了表现开和关状态下的区别,我们再添加一个ImageView,代码如下:
<ToggleButton android:id="@+id/toggleButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:textOff="柯南" android:textOn="犬夜叉" /> //这里我用两张图片表示开和关 //柯南代表关,犬夜叉代表开 //没有设置checked属性,表示启动时的默认状态为关 <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toggleButton" android:background="@drawable/conan" /> //记得先将图片放到drawable文件夹下,我的图片分别命名为conan和inu
(2)配置好布局后,来到MainActivity中添加逻辑代码
首先初始化ToggleButton和ImageView并find:
private ToggleButton togglebutton; private ImageView imageview; //初始化控件 togglebutton = (ToggleButton)findViewById(R.id.toggleButton); imageview = (ImageView)findViewById(R.id.imageView);
然后给ToggleButton设置监听器(需要注意的是这里的监听事件是onCheckedChangeListener而不是onClickListener),这里通过接口来实现:
togglebutton.setOnCheckedChangeListener(this);public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ /* * 当togglebutton被点击的时候,当前的方法会执行 * buttonView----代表被点击控件的本身 * isChecked----代表被点击控件的状态 * * 当点击togglebutton的时候,更换imageview的背景 */ imageview.setBackgroundResource(isChecked?R.drawable.inu:R.drawable.conan);}
3、下面给出MainActivity完整的代码实现:
public class MainActivity extends Activity implements OnCheckedChangeListener{ private ToggleButton togglebutton; private ImageView imageview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 togglebutton = (ToggleButton)findViewById(R.id.toggleButton); imageview = (ImageView)findViewById(R.id.imageView); /* * 给当前的togglebutton设置监听器 */ togglebutton.setOnCheckedChangeListener(this); } public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ /* * 当togglebutton被点击的时候,当前的方法会执行 * buttonView----代表被点击控件的本身 * isChecked----代表被点击控件的状态 * * 当点击togglebutton的时候,更换imagebutton的背景 */ img.setBackgroundResource(isChecked?R.drawable.inu:R.drawable.conan); } }
最后再看一看效果,启动时默认状态为关,显示图片为柯南
按下按钮后,状态变为开,显示图片为犬夜叉
0 0
- 学Android---ToggleButton
- android ToggleButton
- Android-ToggleButton
- 第38章、开关控件Switch和ToggleButton(从零开始学Android)
- 一步一步学android控件(之十八)—— ToggleButton & Switch
- Android ToggleButton example
- Android开关控件ToggleButton
- android控件之ToggleButton
- Android中的ToggleButton
- android ToggleButton的使用
- Android--ToggleButton控件
- Android 自定义ToggleButton
- Android-ToggleButton&CheckTextView&CheckBox
- Android ToggleButton和Switch
- android-----自定义ToggleButton
- Android ToggleButton 实践
- android中的ToggleButton
- Android控件---ToggleButton
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- virtualbox创建com对象失败
- GestureDetector类及其用法
- 贝塞尔曲线的实现
- C#中Trim()、TrimStart()、TrimEnd()的用法
- 学Android---ToggleButton
- 5、数组
- centos7.1 docker安装(内网)
- 正则表达式30分钟入门教程
- Ubuntu下安装mysql
- C/C++文件编辑的总结
- MindMapper的使用技巧分享
- 设计模式-中介者
- Spring Security教程(3)---- 自定义登录页面