一、效果圖
選中:顯示自動更新開啟
不選擇:顯示自動更新關閉
------------
在布局文件中的使用方式和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();}}});}
}