这样看没办法看出效果,如果能做出动态图就好了,下次吧。
除了ToggleButton的自定义之外,用户配置的信息也是要保存起来的,每一次启动程序的时候要能保证使用的是之前的配置,而不是默认配置,在这里使用SharedPreferences是最好的选择了。
想要源码的猛戳这里:http://download.csdn.net/detail/wwj_748/5945829
布局文件:
/2013.08.14_ToggleButton_demo/res/layout/settings.xml
<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” />
<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” />
<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” />
哪些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);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
e);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-8FbOtCYW-1715489262038)]
[外链图片转存中…(img-09XT3iHZ-1715489262041)]
[外链图片转存中…(img-3QWC3m4e-1715489262042)]
[外链图片转存中…(img-6OlMpsTC-1715489262043)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!