您当前的位置: 首页 >  xiangzhihong8

toggbutton

xiangzhihong8 发布时间:2014-01-07 16:26:31 ,浏览量:6

2013年8月14日Android记录

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

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


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

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

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


布局文件:
/2013.08.14_ToggleButton_demo/res/layout/settings.xml
[html] view plain copy print ?
  1.   
  2.   
  3.       
  4.   
  5.           
  6.       
  7.   
  8.       
  9.   
  10.           
  11.   
  12.               
  13.   
  14.                   
  15.   
  16.                   
  17.   
  18.                       
  19.   
  20.                       
  21.                   
  22.               
  23.   
  24.               
  25.   
  26.               
  27.   
  28.                   
  29.   
  30.                   
  31.   
  32.                       
  33.   
  34.                       
  35.                   
  36.               
  37.           
  38.       
  39.   
  40.   


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


Activity文件
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/Setting.java
[java] view plain copy print ?
  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
[java] view plain copy print ?
  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
[java] view plain copy print ?
  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
[java] view plain copy print ?
  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), 0, 0, 0);  
  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), 0, 0, 0);  
  78.             animation.setDuration(200);  
  79.             toggle_Button.startAnimation(animation);  
  80.         }  
  81.     }  
  82.   

关注
打赏
查看更多评论

xiangzhihong8

暂无认证

  • 6浏览

    0关注

    1319博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录