一、效果图
选中:显示自动更新开启
不选择:显示自动更新关闭
------------
在布局文件中的使用方式和android自生的控件一样
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewstyle="@style/ActivityTitle"android:text="@string/settingTitle" /><com.zengmg.MobileSafe.view.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="match_parent"android:layout_height="wrap_content"zengmg:desc_off="自动更新关闭"zengmg:desc_on="自动更新开启"zengmg:title="自动更新设置" /></LinearLayout>
二、步骤
思路:模仿android定义好的控件来写
1、写好布局文件
2、编写自定义 attrs.xml
3、编写自定义控件代码
4、在activity中使用,增加点击事件代码
三、各步骤代码
控件名:SettingItemView
3.1布局文件:activity_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewstyle="@style/ActivityTitle"android:text="@string/settingTitle" /><com.zengmg.MobileSafe.view.SettingItemViewandroid:id="@+id/siv_update"android:layout_width="match_parent"android:layout_height="wrap_content"zengmg:desc_off="自动更新关闭"zengmg:desc_on="自动更新开启"zengmg:title="自动更新设置" /></LinearLayout>
注意:需要加入一个自定义的xml声明:
xmlns:zengmg="http://schemas.android.com/apk/res/com.zengmg.MobileSafe"
声明中的 zengmg 是配置属性时的头。
zengmg:desc_off="自动更新关闭"
zengmg:desc_on="自动更新开启"
zengmg:title="自动更新设置"
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="SettingItemView"><attr name="title" format="string" /><attr name="desc_on" format="string" /><attr name="desc_off" format="string" /></declare-styleable><!-- 在D:\ADT\sdk\platforms\android-16\data\res\values\attrs.xml 文件中找 name="TextView"。name="SettingItemView" 是控件名--></resources>
3.3 自定义控件代码 SettingItemView.java
package com.zengmg.MobileSafe.view;import com.zengmg.MobileSafe.R;import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;/*** 设置页面的自定义控件* 布局文件在:view_setting_item.xml* 属性文件在:values/attrs.xml。(通过查看Android自带的控件找到的)* * @author zengmg* */
public class SettingItemView extends RelativeLayout {private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.zengmg.MobileSafe";private TextView tvTitle;private TextView tvDesc;private CheckBox cbState;private String mtitle;private String mDescOn;private String mDescOff;public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);mtitle=attrs.getAttributeValue(NAMESPACE, "title");mDescOn=attrs.getAttributeValue(NAMESPACE, "desc_on");mDescOff=attrs.getAttributeValue(NAMESPACE, "desc_off");initView();}public SettingItemView(Context context) {super(context);}public void initView(){//View android.view.View.inflate(Context context, int resource, ViewGroup root)//将自定义好的布局文件设置给当前的SettingItemView控件//当SettingItemView控件被使用时,文件view_setting_item里的布局就被显示了View.inflate(getContext(), R.layout.view_setting_item, this);tvTitle=(TextView) findViewById(R.id.tv_title);tvDesc=(TextView) findViewById(R.id.tv_desc);cbState=(CheckBox) findViewById(R.id.cb_status);setMtitle(mtitle);}public String getMtitle() {return tvTitle.getText().toString();}public void setMtitle(String mtitle) {tvTitle.setText(mtitle);}public String getmDescOn() {return tvDesc.getText().toString();}public void setmDescOn(String mDescOn) {tvDesc.setText(mDescOn);}public String getmDescOff() {return tvDesc.getText().toString();}public void setmDescOff(String mDescOff) {tvDesc.setText(mDescOff);}public boolean isChecked() {return cbState.isChecked();}public void setChecked(boolean mIsChecked) {cbState.setChecked(mIsChecked);if(mIsChecked){tvDesc.setText(mDescOn);}else{tvDesc.setText(mDescOff);}}}
有关
View.inflate(getContext(), R.layout.view_setting_item, this);
参见:
http://blog.csdn.net/zengmingen/article/details/49975753
3.4 在activity使用的代码
package com.zengmg.MobileSafe.activity;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;import com.zengmg.MobileSafe.R;
import com.zengmg.MobileSafe.view.SettingItemView;public class SettingActivity extends Activity{private SettingItemView sivUpdate;private SharedPreferences mPref;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_setting);mPref = getSharedPreferences("config", MODE_PRIVATE);sivUpdate=(SettingItemView) findViewById(R.id.siv_update);boolean autoUpdate=mPref.getBoolean("auto_update", true);sivUpdate.setChecked(autoUpdate);sivUpdate.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if(sivUpdate.isChecked()){//不自动更新sivUpdate.setChecked(false);mPref.edit().putBoolean("auto_update", false).commit();}else{//自动更新sivUpdate.setChecked(true);mPref.edit().putBoolean("auto_update", true).commit();}}});}
}