Android记录4--自定义ToggleButton+用SharedPreferences保存用户配置
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_bg" > <TextView
android:id="@+id/tv_Title"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="设置"
android:textColor="#ffffff"
android:textSize="22sp" />
</RelativeLayout> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:id="@+id/layout_AutoPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_short_bg_selector"
android:gravity="center_vertical" > <TextView
android:id="@+id/tv_AutoPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:focusable="false"
android:singleLine="true"
android:text="自动播放"
android:textColor="#7a6f66"
android:textSize="18sp" /> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" > <ToggleButton
android:id="@+id/toggle_AutoPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_selector"
android:gravity="left|center_vertical"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:textColor="#ffffff"
android:textOff="OFF"
android:textOn="ON" /> <ImageButton
android:id="@+id/toggleButton_AutoPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/toggle_AutoPlay"
android:background="#00000000"
android:src="@drawable/progress_thumb_selector" />
</RelativeLayout>
</LinearLayout> <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/list_divider" /> <LinearLayout
android:id="@+id/layout_StartOnBoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/item_short_bg_selector"
android:gravity="center_vertical" > <TextView
android:id="@+id/tv_StartOnBoot"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:focusable="false"
android:singleLine="true"
android:text="开机自启动"
android:textColor="#7a6f66"
android:textSize="18sp" /> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" > <ToggleButton
android:id="@+id/toggle_StartOnBoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toggle_selector"
android:gravity="left|center_vertical"
android:paddingLeft="14dp"
android:paddingRight="14dp"
android:textColor="#ffffff"
android:textOff="OFF"
android:textOn="ON" /> <ImageButton
android:id="@+id/toggleButton_StartOnBoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/toggle_StartOnBoot"
android:background="#00000000"
android:src="@drawable/progress_thumb_selector" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView> </LinearLayout>
package com.wwj.toggle; import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton; /**
* 自定义ToggleButton的例子
*
* @author wwj 2013年8月14
*/
public class Setting extends Activity { private LinearLayout layout_AutoPlay;
private LinearLayout layout_StartOnBoot;
private ToggleButton toggle_AutoPlay;
private ToggleButton toggle_StartOnBoot;
private ImageButton toggleButton_AutoPlay;
private ImageButton toggleButton_StartOnBoot; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings); // 找到控件
layout_AutoPlay = (LinearLayout) findViewById(R.id.layout_AutoPlay);
layout_StartOnBoot = (LinearLayout) findViewById(R.id.layout_StartOnBoot);
toggle_AutoPlay = (ToggleButton) findViewById(R.id.toggle_AutoPlay);
toggle_StartOnBoot = (ToggleButton) findViewById(R.id.toggle_StartOnBoot);
toggleButton_AutoPlay = (ImageButton) findViewById(R.id.toggleButton_AutoPlay);
toggleButton_StartOnBoot = (ImageButton) findViewById(R.id.toggleButton_StartOnBoot); initViews();
setListeners();
} private void initViews() {
// 是否自动播放,获取SharePerference保存的用户配置
boolean isAutoPlay = SettingUtils.get(this, SettingUtils.AUTO_PLAY,
false);
toggle_AutoPlay.setChecked(isAutoPlay);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggleButton_AutoPlay
.getLayoutParams();
if (isAutoPlay) { // 如果是自动播放
// 调整位置
params.addRule(RelativeLayout.ALIGN_RIGHT, -1);
params.addRule(RelativeLayout.ALIGN_LEFT,
R.id.toggleButton_AutoPlay);
toggleButton_AutoPlay.setLayoutParams(params);
toggleButton_AutoPlay
.setImageResource(R.drawable.progress_thumb_selector);
toggle_AutoPlay.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
} else {
// 调整位置
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);
params.addRule(RelativeLayout.ALIGN_LEFT, -1);
toggleButton_AutoPlay.setLayoutParams(params);
toggleButton_AutoPlay
.setImageResource(R.drawable.progress_thumb_off_selector);
toggle_AutoPlay.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
} boolean isAutostart = SettingUtils.get(this,
SettingUtils.IS_AUTO_START, true); toggle_StartOnBoot.setChecked(isAutostart);
RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams) toggleButton_StartOnBoot
.getLayoutParams();
if (isAutostart) {
// 调整位置
params3.addRule(RelativeLayout.ALIGN_RIGHT, -1);
params3.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_StartOnBoot);
toggleButton_StartOnBoot.setLayoutParams(params3);
toggleButton_StartOnBoot
.setImageResource(R.drawable.progress_thumb_selector); toggle_StartOnBoot.setGravity(Gravity.RIGHT
| Gravity.CENTER_VERTICAL);
} else {
// 调整位置
params3.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_StartOnBoot);
params3.addRule(RelativeLayout.ALIGN_LEFT, -1);
toggleButton_StartOnBoot.setLayoutParams(params3);
toggleButton_StartOnBoot
.setImageResource(R.drawable.progress_thumb_off_selector); toggle_StartOnBoot.setGravity(Gravity.LEFT
| Gravity.CENTER_VERTICAL);
}
} private void setListeners() {
toggle_AutoPlay.setOnCheckedChangeListener(new ToggleListener(this,
"自动播放", toggle_AutoPlay, toggleButton_AutoPlay));
toggle_StartOnBoot.setOnCheckedChangeListener(new ToggleListener(this,
"开机自启动", toggle_StartOnBoot, toggleButton_StartOnBoot)); // UI事件,按钮点击事件
OnClickListener clickToToggleListener = new OnClickListener() { @Override
public void onClick(View v) {
toggle_AutoPlay.toggle();
}
}; toggleButton_AutoPlay.setOnClickListener(clickToToggleListener);
layout_AutoPlay.setOnClickListener(clickToToggleListener); // UI事件,按钮点击事件
OnClickListener clickToToggleAutostartListener = new OnClickListener() {
public void onClick(View v) {
toggle_StartOnBoot.toggle();
}
};
toggleButton_StartOnBoot
.setOnClickListener(clickToToggleAutostartListener);
layout_StartOnBoot
.setOnClickListener(clickToToggleAutostartListener);
} }
工具类:
package com.wwj.toggle; import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager; public class SettingUtils {
public static final String AUTO_PLAY = "auto_play"; // 自动播放
public static final String IS_AUTO_START = "is_auto_start"; // 开机自启动 /**
* 获取配置
* @param context
* @param name
* @param defaultValue
* @return
*/
public static boolean get(Context context, String name, boolean defaultValue) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean value = prefs.getBoolean(name, defaultValue);
return value;
} /**
* 保存用户配置
* @param context
* @param name
* @param value
* @return
*/
public static boolean set(Context context, String name, boolean value) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
editor.putBoolean(name, value);
return editor.commit(); //提交
}
}
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/DisplayUtils.java
package com.wwj.toggle;
import android.content.Context;
public class DisplayUtils {
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
}
package com.wwj.toggle; import android.content.Context;
import android.view.Gravity;
import android.view.animation.TranslateAnimation;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.ToggleButton; /**
* 状态按钮的监听事件
*
* @author wwj
*
*/
public class ToggleListener implements OnCheckedChangeListener {
private Context context;
private String settingName;
private ToggleButton toggle;
private ImageButton toggle_Button; public ToggleListener(Context context, String settingName,
ToggleButton toggle, ImageButton toggle_Button) {
this.context = context;
this.settingName = settingName;
this.toggle = toggle;
this.toggle_Button = toggle_Button;
} @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 保存设置
if ("自动播放".equals(settingName)) {
SettingUtils.set(context, SettingUtils.AUTO_PLAY, isChecked);
} else if ("开机自启动".equals(settingName)) {
SettingUtils.set(context, SettingUtils.IS_AUTO_START, isChecked);
}
// 播放动画
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggle_Button
.getLayoutParams();
if (isChecked) {
// 调整位置
params.addRule(RelativeLayout.ALIGN_RIGHT, -1);
if ("自动播放".equals(settingName)) {
params.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_AutoPlay);
} else if ("开机自启动".equals(settingName)) {
params.addRule(RelativeLayout.ALIGN_LEFT,
R.id.toggle_StartOnBoot);
}
toggle_Button.setLayoutParams(params);
toggle_Button.setImageResource(R.drawable.progress_thumb_selector);
toggle.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
// 播放动画
TranslateAnimation animation = new TranslateAnimation(
DisplayUtils.dip2px(context, 40), 0, 0, 0);
animation.setDuration(200);
toggle_Button.startAnimation(animation);
} else {
// 调整位置
if ("自动播放".equals(settingName)) {
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_AutoPlay);
} else if ("开机自启动".equals(settingName)) {
params.addRule(RelativeLayout.ALIGN_RIGHT,
R.id.toggle_StartOnBoot);
}
params.addRule(RelativeLayout.ALIGN_LEFT, -1);
toggle_Button.setLayoutParams(params);
toggle_Button
.setImageResource(R.drawable.progress_thumb_off_selector); toggle.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
// 播放动画
TranslateAnimation animation = new TranslateAnimation(
DisplayUtils.dip2px(context, -40), 0, 0, 0);
animation.setDuration(200);
toggle_Button.startAnimation(animation);
}
} }
Android记录4--自定义ToggleButton+用SharedPreferences保存用户配置的更多相关文章
- Android 自定义ToggleButton+用SharedPreferences保存用户配置
布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...
- Android之使用SharedPreferences保存用户偏好参数
在Android应用中,我们常需要记录用户设置的一些偏好参数,,此时我们就需要用SharedPreferences和Editor将这些信息保存下来,在下次登录时读取. SharedPreference ...
- 黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数
SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限: 我们来看看 我们来看看具体的业务操作类: /** * 文件名:SharedPrecences.java * ...
- 使用SharedPreferences存储用户配置信息
用SharedPreferences来保存用户的基本配置信息非常的方便,实现起来也很容易:以下是一个简单的例子: 效果截图: 主要代码: public class MainActivity ex ...
- 16_采用SharedPreferences保存用户偏好设置参数
按钮事件 <Button android:id="@+id/button" android:layout_width="wrap_content" and ...
- SharedPreferences保存用户登录信息
UI界面:
- SharedPreferences保存用户偏好参数
package com.example.administrator.myapplication; import android.content.Context; import android.cont ...
- Servlet之保存用户偏好设置简单功能的实现
写在前面: 先来陈述一下为什么会有这样一个需求和这篇博文. 这是公司的一个项目,我们负责前端,后台服务由其他公司负责.该系统有一个系统偏好设置模块,用户可以设置系统的背景图片等系统样式,因为这是一个比 ...
- Android SharedPreferences保存和读取对象
SharedPreferences保存和读取对象 1.首先要序列化对象(以下是序列化实体类的样例,不方便贴出实体类全部的代码) public class LoginResult extends Bas ...
随机推荐
- 转 --maven系列之二 安装与配置
http://blog.csdn.net/jiuqiyuliang/article/details/45390313 [项目管理和构建]——Maven下载.安装和配置(二) 标签: 工具开发maven ...
- Maven java项目管理工具
Maven java项目管理工具 1.安装maven 下载最新的maven 下载地址 http://maven.apache.org/download.cgi 传到要安装的目录 例如/opt/下 # ...
- USACO Section 4.3 Street Race(图的连通性+枚举)
虽说是IOI'95,但是也是挺水的..for 第一问,n最大为50,所以可以直接枚举起点和终点之外的所有点,然后dfs判断是否连通:for 第二问,易知答案一定是第一问的子集,所以从第一问中的答案中枚 ...
- Android 一个抽奖应用的逆向破解全流程之加固自己应用
转自: <a href="http://www.pedant.cn/2014/07/22/crack-a-draw-app/">http://www.pedant.cn ...
- 美化 input type=file控件
大家都知道input的type=file控件默认样式是不能修改的 可以通过一个小技巧处理下 html: <a href="javascript:;" class=" ...
- Python3 关于UnicodeDecodeError/UnicodeEncodeError: ‘gbk’ codec can’t decode/encode bytes类似的文本编码问题
以下是小白的爬虫学习历程中遇到并解决的一些困难,希望写出来给后来人,如有疏漏恳请大牛指正,不胜感谢! 首先,我的代码是这样的 import requests url = 'http://www.acf ...
- tomcat部署不正确
我把一个名为cp的web项目导入myeclipse中,由于要再次导入该工程的新版本,我就把旧版本的cp项目改名成cp3.但是在运行新项目cp的时候,tomcat出问题,一直是之前旧cp对应的tomca ...
- 红外摄像头为什么使用850nm波长红外发射管
市面上有很多不同波长的红外发射管,其中以850nm和940nm波长为主.那么红外摄像头为什么使用850nm波长红外发射管? 首先,我们来了解一下红外摄像头的相关知识.简单来说红外摄像头是用来感应红外线 ...
- cocos2dx CCControlSwitch
CCControlSwitch也是extension中的控件,本身比较简单,直接上例子 // on "init" you need to initialize your insta ...
- 51nod 1237 最大公约数之和 V3(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...