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” />
哪些selector文件的代码就不贴了,自己看源码吧
Activity文件
**/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/Setting.java
**
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);
}
}
工具类:
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/SettingUtils.java
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;
}
}
/2013.08.14_ToggleButton_demo/src/com/wwj/toggle/ToggleListener.java
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)) {
最后
对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!
最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
以下是今天给大家分享的一些独家干货:
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
是环境来适应我们!**
最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。
以下是今天给大家分享的一些独家干货:
[外链图片转存中…(img-Y7Vv2k8s-1714456565881)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!