toggbutton

简介: <div style="text-align:left"><span style="font-family:FangSong_GB2312; font-size:18px">2013年8月14日<span style="white-space:pre"></span>Android记录</span></div> <div style="text-align:left"><span sty
2013年8月14日Android记录

很多应用都会有用户设置,用户的一些偏好可以由用户来决定那是应用人性化的体现,在实际开发中很多情况都作成可配置的了,本篇博客要介绍的是一个比较炫的状态按钮切换,我想很多开发者都想做出这样的效果,在这里我也就把自己参与的项目当中的这部分实现,做出Demo来于朋友们分享。

没有图,我感觉就特别不舒服:


这样看没办法看出效果,如果能做出动态图就好了,下次吧。

除了ToggleButton的自定义之外,用户配置的信息也是要保存起来的,每一次启动程序的时候要能保证使用的是之前的配置,而不是默认配置,在这里使用SharedPreferences是最好的选择了。

想要源码的猛戳这里:http://download.csdn.net/detail/wwj_748/5945829


布局文件:
/2013.08.14_ToggleButton_demo/res/layout/settings.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/LinearLayout1"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="@drawable/banner_bg" >  
  12.   
  13.         <TextView  
  14.             android:id="@+id/tv_Title"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="42dp"  
  17.             android:layout_centerHorizontal="true"  
  18.             android:gravity="center"  
  19.             android:text="设置"  
  20.             android:textColor="#ffffff"  
  21.             android:textSize="22sp" />  
  22.     </RelativeLayout>  
  23.   
  24.     <ScrollView  
  25.         android:layout_width="match_parent"  
  26.         android:layout_height="match_parent" >  
  27.   
  28.         <LinearLayout  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="wrap_content"  
  31.             android:orientation="vertical" >  
  32.   
  33.             <LinearLayout  
  34.                 android:id="@+id/layout_AutoPlay"  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="wrap_content"  
  37.                 android:background="@drawable/item_short_bg_selector"  
  38.                 android:gravity="center_vertical" >  
  39.   
  40.                 <TextView  
  41.                     android:id="@+id/tv_AutoPlay"  
  42.                     android:layout_width="0dp"  
  43.                     android:layout_height="wrap_content"  
  44.                     android:layout_marginLeft="10dp"  
  45.                     android:layout_weight="1"  
  46.                     android:focusable="false"  
  47.                     android:singleLine="true"  
  48.                     android:text="自动播放"  
  49.                     android:textColor="#7a6f66"  
  50.                     android:textSize="18sp" />  
  51.   
  52.                 <RelativeLayout  
  53.                     android:layout_width="wrap_content"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:layout_marginRight="10dp" >  
  56.   
  57.                     <ToggleButton  
  58.                         android:id="@+id/toggle_AutoPlay"  
  59.                         android:layout_width="wrap_content"  
  60.                         android:layout_height="wrap_content"  
  61.                         android:background="@drawable/toggle_selector"  
  62.                         android:gravity="left|center_vertical"  
  63.                         android:paddingLeft="14dp"  
  64.                         android:paddingRight="14dp"  
  65.                         android:textColor="#ffffff"  
  66.                         android:textOff="OFF"  
  67.                         android:textOn="ON" />  
  68.   
  69.                     <ImageButton  
  70.                         android:id="@+id/toggleButton_AutoPlay"  
  71.                         android:layout_width="wrap_content"  
  72.                         android:layout_height="wrap_content"  
  73.                         android:layout_alignRight="@+id/toggle_AutoPlay"  
  74.                         android:background="#00000000"  
  75.                         android:src="@drawable/progress_thumb_selector" />  
  76.                 </RelativeLayout>  
  77.             </LinearLayout>  
  78.   
  79.             <ImageView  
  80.                 android:layout_width="match_parent"  
  81.                 android:layout_height="wrap_content"  
  82.                 android:scaleType="fitXY"  
  83.                 android:src="@drawable/list_divider" />  
  84.   
  85.             <LinearLayout  
  86.                 android:id="@+id/layout_StartOnBoot"  
  87.                 android:layout_width="match_parent"  
  88.                 android:layout_height="wrap_content"  
  89.                 android:background="@drawable/item_short_bg_selector"  
  90.                 android:gravity="center_vertical" >  
  91.   
  92.                 <TextView  
  93.                     android:id="@+id/tv_StartOnBoot"  
  94.                     android:layout_width="0dp"  
  95.                     android:layout_height="wrap_content"  
  96.                     android:layout_marginLeft="10dp"  
  97.                     android:layout_weight="1"  
  98.                     android:focusable="false"  
  99.                     android:singleLine="true"  
  100.                     android:text="开机自启动"  
  101.                     android:textColor="#7a6f66"  
  102.                     android:textSize="18sp" />  
  103.   
  104.                 <RelativeLayout  
  105.                     android:layout_width="wrap_content"  
  106.                     android:layout_height="wrap_content"  
  107.                     android:layout_marginRight="10dp" >  
  108.   
  109.                     <ToggleButton  
  110.                         android:id="@+id/toggle_StartOnBoot"  
  111.                         android:layout_width="wrap_content"  
  112.                         android:layout_height="wrap_content"  
  113.                         android:background="@drawable/toggle_selector"  
  114.                         android:gravity="left|center_vertical"  
  115.                         android:paddingLeft="14dp"  
  116.                         android:paddingRight="14dp"  
  117.                         android:textColor="#ffffff"  
  118.                         android:textOff="OFF"  
  119.                         android:textOn="ON" />  
  120.   
  121.                     <ImageButton  
  122.                         android:id="@+id/toggleButton_StartOnBoot"  
  123.                         android:layout_width="wrap_content"  
  124.                         android:layout_height="wrap_content"  
  125.                         android:layout_alignRight="@+id/toggle_StartOnBoot"  
  126.                         android:background="#00000000"  
  127.                         android:src="@drawable/progress_thumb_selector" />  
  128.                 </RelativeLayout>  
  129.             </LinearLayout>  
  130.         </LinearLayout>  
  131.     </ScrollView>  
  132.   
  133. </LinearLayout>  


哪些selector文件的代码就不贴了,自己看源码吧


Activity文件
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/Setting.java
  1. package com.wwj.toggle;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.ImageButton;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.RelativeLayout;  
  12. import android.widget.ToggleButton;  
  13.   
  14. /** 
  15.  * 自定义ToggleButton的例子 
  16.  *  
  17.  * @author wwj 2013年8月14 
  18.  */  
  19. public class Setting extends Activity {  
  20.   
  21.     private LinearLayout layout_AutoPlay;  
  22.     private LinearLayout layout_StartOnBoot;  
  23.     private ToggleButton toggle_AutoPlay;  
  24.     private ToggleButton toggle_StartOnBoot;  
  25.     private ImageButton toggleButton_AutoPlay;  
  26.     private ImageButton toggleButton_StartOnBoot;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.settings);  
  32.   
  33.         // 找到控件  
  34.         layout_AutoPlay = (LinearLayout) findViewById(R.id.layout_AutoPlay);  
  35.         layout_StartOnBoot = (LinearLayout) findViewById(R.id.layout_StartOnBoot);  
  36.         toggle_AutoPlay = (ToggleButton) findViewById(R.id.toggle_AutoPlay);  
  37.         toggle_StartOnBoot = (ToggleButton) findViewById(R.id.toggle_StartOnBoot);  
  38.         toggleButton_AutoPlay = (ImageButton) findViewById(R.id.toggleButton_AutoPlay);  
  39.         toggleButton_StartOnBoot = (ImageButton) findViewById(R.id.toggleButton_StartOnBoot);  
  40.   
  41.         initViews();  
  42.         setListeners();  
  43.     }  
  44.   
  45.     private void initViews() {  
  46.         // 是否自动播放,获取SharePerference保存的用户配置  
  47.         boolean isAutoPlay = SettingUtils.get(this, SettingUtils.AUTO_PLAY,  
  48.                 false);  
  49.         toggle_AutoPlay.setChecked(isAutoPlay);  
  50.         RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggleButton_AutoPlay  
  51.                 .getLayoutParams();  
  52.         if (isAutoPlay) { // 如果是自动播放  
  53.             // 调整位置  
  54.             params.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  55.             params.addRule(RelativeLayout.ALIGN_LEFT,  
  56.                     R.id.toggleButton_AutoPlay);  
  57.             toggleButton_AutoPlay.setLayoutParams(params);  
  58.             toggleButton_AutoPlay  
  59.                     .setImageResource(R.drawable.progress_thumb_selector);  
  60.             toggle_AutoPlay.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);  
  61.         } else {  
  62.             // 调整位置  
  63.             params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);  
  64.             params.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  65.             toggleButton_AutoPlay.setLayoutParams(params);  
  66.             toggleButton_AutoPlay  
  67.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  68.             toggle_AutoPlay.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);  
  69.         }  
  70.   
  71.         boolean isAutostart = SettingUtils.get(this,  
  72.                 SettingUtils.IS_AUTO_START, true);  
  73.   
  74.         toggle_StartOnBoot.setChecked(isAutostart);  
  75.         RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) toggleButton_StartOnBoot  
  76.                 .getLayoutParams();  
  77.         if (isAutostart) {  
  78.             // 调整位置  
  79.             params3.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  80.             params3.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_StartOnBoot);  
  81.             toggleButton_StartOnBoot.setLayoutParams(params3);  
  82.             toggleButton_StartOnBoot  
  83.                     .setImageResource(R.drawable.progress_thumb_selector);  
  84.   
  85.             toggle_StartOnBoot.setGravity(Gravity.RIGHT  
  86.                     | Gravity.CENTER_VERTICAL);  
  87.         } else {  
  88.             // 调整位置  
  89.             params3.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_StartOnBoot);  
  90.             params3.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  91.             toggleButton_StartOnBoot.setLayoutParams(params3);  
  92.             toggleButton_StartOnBoot  
  93.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  94.   
  95.             toggle_StartOnBoot.setGravity(Gravity.LEFT  
  96.                     | Gravity.CENTER_VERTICAL);  
  97.         }  
  98.     }  
  99.   
  100.     private void setListeners() {  
  101.         toggle_AutoPlay.setOnCheckedChangeListener(new ToggleListener(this,  
  102.                 "自动播放", toggle_AutoPlay, toggleButton_AutoPlay));  
  103.         toggle_StartOnBoot.setOnCheckedChangeListener(new ToggleListener(this,  
  104.                 "开机自启动", toggle_StartOnBoot, toggleButton_StartOnBoot));  
  105.   
  106.         // UI事件,按钮点击事件  
  107.         OnClickListener clickToToggleListener = new OnClickListener() {  
  108.   
  109.             @Override  
  110.             public void onClick(View v) {  
  111.                 toggle_AutoPlay.toggle();  
  112.             }  
  113.         };  
  114.   
  115.         toggleButton_AutoPlay.setOnClickListener(clickToToggleListener);  
  116.         layout_AutoPlay.setOnClickListener(clickToToggleListener);  
  117.   
  118.         // UI事件,按钮点击事件  
  119.         OnClickListener clickToToggleAutostartListener = new OnClickListener() {  
  120.             public void onClick(View v) {  
  121.                 toggle_StartOnBoot.toggle();  
  122.             }  
  123.         };  
  124.         toggleButton_StartOnBoot  
  125.                 .setOnClickListener(clickToToggleAutostartListener);  
  126.         layout_StartOnBoot  
  127.                 .setOnClickListener(clickToToggleAutostartListener);  
  128.     }  
  129.   
  130. }  

工具类:
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/SettingUtils.java
  1. package com.wwj.toggle;  
  2.   
  3. import android.content.Context;  
  4. import android.content.SharedPreferences;  
  5. import android.content.SharedPreferences.Editor;  
  6. import android.preference.PreferenceManager;  
  7.   
  8. public class SettingUtils {  
  9.     public static final String AUTO_PLAY = "auto_play"// 自动播放  
  10.     public static final String IS_AUTO_START = "is_auto_start"// 开机自启动  
  11.       
  12.       
  13.     /** 
  14.      * 获取配置 
  15.      * @param context 
  16.      * @param name 
  17.      * @param defaultValue 
  18.      * @return 
  19.      */  
  20.     public static boolean get(Context context, String name, boolean defaultValue) {  
  21.         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  
  22.         boolean value = prefs.getBoolean(name, defaultValue);  
  23.         return value;  
  24.     }  
  25.       
  26.     /** 
  27.      * 保存用户配置 
  28.      * @param context 
  29.      * @param name 
  30.      * @param value 
  31.      * @return 
  32.      */  
  33.     public static boolean set(Context context, String name, boolean value) {  
  34.         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);  
  35.         Editor editor = prefs.edit();  
  36.         editor.putBoolean(name, value);  
  37.         return editor.commit(); //提交  
  38.     }  
  39. }  

/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/DisplayUtils.java
  1. package com.wwj.toggle;  
  2.   
  3. import android.content.Context;  
  4.   
  5. public class DisplayUtils {  
  6.     public static int dip2px(Context context, float dpValue) {  
  7.         final float scale = context.getResources().getDisplayMetrics().density;  
  8.         return (int) (dpValue * scale + 0.5f);  
  9.     }  
  10.   
  11.     public static int px2dip(Context context, float pxValue) {  
  12.         final float scale = context.getResources().getDisplayMetrics().density;  
  13.         return (int) (pxValue / scale + 0.5f);  
  14.     }  
  15.   
  16.     public static int getScreenWidth(Context context) {  
  17.         return context.getResources().getDisplayMetrics().widthPixels;  
  18.     }  
  19.   
  20.     public static int getScreenHeight(Context context) {  
  21.         return context.getResources().getDisplayMetrics().heightPixels;  
  22.     }  
  23. }  


/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/ToggleListener.java
  1. package com.wwj.toggle;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.animation.TranslateAnimation;  
  7. import android.widget.CompoundButton;  
  8. import android.widget.CompoundButton.OnCheckedChangeListener;  
  9. import android.widget.ImageButton;  
  10. import android.widget.RelativeLayout;  
  11. import android.widget.ToggleButton;  
  12.   
  13. /** 
  14.  * 状态按钮的监听事件 
  15.  *  
  16.  * @author wwj 
  17.  *  
  18.  */  
  19. public class ToggleListener implements OnCheckedChangeListener {  
  20.     private Context context;  
  21.     private String settingName;  
  22.     private ToggleButton toggle;  
  23.     private ImageButton toggle_Button;  
  24.   
  25.     public ToggleListener(Context context, String settingName,  
  26.             ToggleButton toggle, ImageButton toggle_Button) {  
  27.         this.context = context;  
  28.         this.settingName = settingName;  
  29.         this.toggle = toggle;  
  30.         this.toggle_Button = toggle_Button;  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  35.         // 保存设置  
  36.         if ("自动播放".equals(settingName)) {  
  37.             SettingUtils.set(context, SettingUtils.AUTO_PLAY, isChecked);  
  38.         } else if ("开机自启动".equals(settingName)) {  
  39.             SettingUtils.set(context, SettingUtils.IS_AUTO_START, isChecked);  
  40.         }  
  41.         // 播放动画  
  42.         RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggle_Button  
  43.                 .getLayoutParams();  
  44.         if (isChecked) {  
  45.             // 调整位置  
  46.             params.addRule(RelativeLayout.ALIGN_RIGHT, -1);  
  47.             if ("自动播放".equals(settingName)) {  
  48.                 params.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_AutoPlay);  
  49.             } else if ("开机自启动".equals(settingName)) {  
  50.                 params.addRule(RelativeLayout.ALIGN_LEFT,  
  51.                         R.id.toggle_StartOnBoot);  
  52.             }  
  53.             toggle_Button.setLayoutParams(params);  
  54.             toggle_Button.setImageResource(R.drawable.progress_thumb_selector);  
  55.             toggle.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);  
  56.             // 播放动画  
  57.             TranslateAnimation animation = new TranslateAnimation(  
  58.                     DisplayUtils.dip2px(context, 40), 000);  
  59.             animation.setDuration(200);  
  60.             toggle_Button.startAnimation(animation);  
  61.         } else {  
  62.             // 调整位置  
  63.             if ("自动播放".equals(settingName)) {  
  64.                 params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);  
  65.             } else if ("开机自启动".equals(settingName)) {  
  66.                 params.addRule(RelativeLayout.ALIGN_RIGHT,  
  67.                         R.id.toggle_StartOnBoot);  
  68.             }  
  69.             params.addRule(RelativeLayout.ALIGN_LEFT, -1);  
  70.             toggle_Button.setLayoutParams(params);  
  71.             toggle_Button  
  72.                     .setImageResource(R.drawable.progress_thumb_off_selector);  
  73.   
  74.             toggle.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);  
  75.             // 播放动画  
  76.             TranslateAnimation animation = new TranslateAnimation(  
  77.                     DisplayUtils.dip2px(context, -40), 000);  
  78.             animation.setDuration(200);  
  79.             toggle_Button.startAnimation(animation);  
  80.         }  
  81.     }  
  82.   

目录
相关文章
|
前端开发 Java Linux
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
1754 0
《Linux篇》02.超详细SpringBoot项目部署教程(附脚本自动部署)(三)
|
JavaScript 前端开发 Python
CGI编程完全手册
一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Web服务器主机提供信息服务的标准接口。通过CGI接口,Web服务器就能够获取客户端提交的信息,转交给服务器端的CGI程序进行处理,最后返回结果给客户端。
2824 0
|
4月前
|
机器学习/深度学习 人工智能 PyTorch
【传知代码】Modnet 人像抠图-论文复现
MODNet是一种轻量级、实时的无trimap人像抠图模型,强调高效和鲁棒性。它由语义预测、细节预测和语义-细节混合三个模块构成,通过自监督学习策略SOC保持模块间一致性。MODNet在人像抠图任务中表现出色,适用于多种场景,如展馆互动、商场引流、图像编辑、广告制作等。论文和源码可在给定链接中获取,项目还提供了WebUI方便用户直接使用。
109 2
【传知代码】Modnet 人像抠图-论文复现
|
C#
C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法
原文:C# 中串口通信 serialport1.DataReceived 函数无法触发或者出发延时等等问题解决方法 以前这个问题困扰我多天最后查资料一大堆,最后最终攻克了,看到非常多人做C#串口都遇到相同的问题,所以写一篇博文,以便学习交流。
3392 0
|
4月前
|
设计模式 JavaScript 前端开发
最常见的26个JavaScript面试题和答案
最常见的26个JavaScript面试题和答案
102 1
|
4月前
GEE:获取sentinel2指定区域多个单景影像的值(样本点提取)
GEE:获取sentinel2指定区域多个单景影像的值(样本点提取)
124 0
|
4月前
|
前端开发 JavaScript Java
基于SSM框架宠物管理系统
基于SSM框架宠物管理系统
122 0
|
12月前
|
Java 编译器
【javaSE】 抽象类
【javaSE】 抽象类
|
SQL Java
MyBatisPlus实现分页和查询操作就这么简单
MyBatisPlus实现分页和查询操作就这么简单
184 0
|
运维 Prometheus 监控
产品动态丨阿里云计算巢月刊-2023年第04期
计算巢支持对服务实例的批量升级、全托管服务支持自定义运维项等7项功能更新;计算巢私有化部署接入Prometheus监控最佳实践;鸿翼医药智能RA平台、可道云网盘Kodbox及数据可视化开放平台datart等合作伙伴服务更新……让优秀的企业软件生于云、长于云~
产品动态丨阿里云计算巢月刊-2023年第04期