最后实现的效果如图:
这里是通过自定义组合控件实现的。先看布局:
activity_setting的布局文件为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f0eb"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="46dp"
android:background="#2FD5B9"
android:gravity="center"
android:text="设置"
android:textColor="#ffffff"
android:textSize="20sp" />
<com.ustc.mobilemanager.ui.SettingItemView
android:id="@+id/siv_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
其中:com.ustc.mobilemanager.ui.SettingItemView这个类为:
package com.ustc.mobilemanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ustc.mobilemanager.R;
/**
*
* 自定义组合控件(两个TextView 一个CheckBox)
*
* @author
*
*/
public class SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_desc;
private TextView tv_title;
private void initView(Context context) {
// 把一个布局文件转化为----》View并且加载在SettingItemView类中
View view = View.inflate(context, R.layout.setting_item_view,
SettingItemView.this);
cb_status = (CheckBox) this.findViewById(R.id.cb_status);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) this.findViewById(R.id.tv_title);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
/**
*
* 校验组合控件是否选中
*
*/
public boolean isChecked() {
return cb_status.isChecked();
}
/**
*
*
* 设置组合控件的状态
*/
public void setChecked(boolean checked) {
cb_status.setChecked(checked);
}
/**
* 组合控件的描述信息
*
*/
public void setDesc(String text) {
tv_desc.setText(text);
}
}
他所加载的布局为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:text="设置是否自动更新"
android:textColor="#99000000"
android:textSize="20sp" />
<TextView
android:layout_below="@id/tv_title"
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="2dip"
android:text="自动更新已经关闭"
android:textColor="#66000000"
android:textSize="16sp" />
<CheckBox
android:clickable="false"
android:id="@+id/cb_status"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:background="#88000000"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
然后,我们需要为CheckBox摄者这样一个属性:
android:clickable="false"
这样就可以取消他的点击属性了。(忘了?赶紧去看看视频吧。)
package com.ustc.mobilemanager;
import com.ustc.mobilemanager.ui.SettingItemView;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
public class SettingActivity extends Activity {
private SettingItemView siv_update;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_setting);
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
siv_update = (SettingItemView) findViewById(R.id.siv_update);
boolean update = sp.getBoolean("update", false);
if (update) {
//自动升级已经开启
siv_update.setChecked(true);
siv_update.setDesc("自动升级已经开启");
}else {
//自动升级已经关闭
siv_update.setChecked(false);
siv_update.setDesc("自动升级已经关闭");
}
siv_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = sp.edit();
// 判断是否选中
// 已经选中了
if (siv_update.isChecked()) {
siv_update.setChecked(false);
siv_update.setDesc("自动升级已经关闭");
editor.putBoolean("update", false);
} else {
// 没有选中
siv_update.setChecked(true);
siv_update.setDesc("自动升级已经开启");
editor.putBoolean("update", true);
}
editor.commit();
}
});
}
}
当我们取消升级的时候,需要去掉SplashActivity界面的下载进度的提示:
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
tv_splash_version = (TextView) findViewById(R.id.tv_splash_version);
rootLayout = (RelativeLayout) findViewById(R.id.rl_root);
tv_update_info = (TextView) findViewById(R.id.tv_update_info);
// 设置版本号
tv_splash_version.setText("版本号:" + getVersion());
boolean update = sp.getBoolean("update", false);
if (update) {
// 检查升级
checkUpdate();
} else {
// 自动升级已经关闭
handler.postDelayed(new Runnable() {
@Override
public void run() {
enterHome();
}
}, 2000);
}
tv_update_info.setVisibility(View.VISIBLE);
// 当前下载的百分比
int progress = (int) (current * 100 / count);
tv_update_info.setText("下载进度:" + progress
+ "%");
默认状态下,这个是不可见的。