在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android ToggleButton 开关按钮实例源码

android ToggleButton 开关按钮实例源码

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.29M
  • 下载次数:17
  • 浏览次数:250
  • 发布时间:2017-02-10
  • 实例类别:Android平台开发
  • 发 布 人:liumin
  • 文件格式:.rar
  • 所需积分:1
 相关标签: ToggleButton 开关

实例介绍

【实例简介】

ToggleButton开关


【实例截图】

【核心代码】


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.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;


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();	//提交
	}
}




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);
		}
	}

}




标签: ToggleButton 开关

实例下载地址

android ToggleButton 开关按钮实例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警