前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于安卓开发实现双状态按钮

关于安卓开发实现双状态按钮

作者头像
听着music睡
发布2018-05-18 12:20:40
7370
发布2018-05-18 12:20:40
举报
文章被收录于专栏:Android干货Android干货

双状态按钮(Toggle)

xml文件代码

代码语言:javascript
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <ImageView android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:src="@drawable/bulb_off"
 9         android:id="@+id/imageView"
10         android:layout_gravity="center_horizontal"
11         />
12     <ToggleButton android:layout_width="140dip"
13         android:layout_height="wrap_content"
14         android:textOn="打开"
15         android:textOff="关闭"
16         android:id="@+id/toggleButton"
17         android:layout_gravity="center_horizontal"
18         />
19 
20 </LinearLayout>

java文件

代码语言:javascript
复制
 1 package lianxi;
 2 
 3 import java.security.PublicKey;
 4 
 5 import com.example.jichu_lianxi.R;
 6 
 7 import android.app.Activity;
 8 import android.os.Bundle;
 9 import android.widget.CompoundButton;
10 import android.widget.CompoundButton.OnCheckedChangeListener;
11 import android.widget.ImageView;
12 import android.widget.ToggleButton;
13 /*
14  * 双按钮
15  */
16 public class ToggleButton_lianxi extends Activity{
17     private ImageView imageView = null;                  //图片标签
18     private ToggleButton toggleButton = null;            //双状态按钮
19     
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // TODO Auto-generated method stub
23         super.onCreate(savedInstanceState);
24     setContentView(R.layout.togglebutton_lianxi);
25     
26     imageView = (ImageView) findViewById(R.id.imageView);
27     toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
28     
29     //设置按钮响应
30     toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
31         
32         @Override
33         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
34             // TODO Auto-generated method stub
35             toggleButton.setChecked(isChecked);
36             //设置图片
37             imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
38         }
39     });
40     
41     }
42         
43 }

效果图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-04-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
内容识别
内容识别(Content Recognition,CR)是腾讯云数据万象推出的对图片内容进行识别、理解的服务,集成腾讯云 AI 的多种强大功能,对存储在腾讯云对象存储 COS 的数据提供图片标签、图片修复、二维码识别、语音识别、质量评估等增值服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档