深圳微商城网站制作联系电话,自己做网站 怎样下载模板,贵阳网站建设服务公司,网站设计的硬件一、效果图 选中#xff1a;显示自动更新开启不选择#xff1a;显示自动更新关闭------------在布局文件中的使用方式和android自生的控件一样 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.andro…一、效果图 选中显示自动更新开启不选择显示自动更新关闭------------在布局文件中的使用方式和android自生的控件一样 ?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:zengmghttp://schemas.android.com/apk/res/com.zengmg.MobileSafeandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewstylestyle/ActivityTitleandroid:textstring/settingTitle /com.zengmg.MobileSafe.view.SettingItemViewandroid:idid/siv_updateandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentzengmg:desc_off自动更新关闭zengmg:desc_on自动更新开启zengmg:title自动更新设置 //LinearLayout 二、步骤
思路模仿android定义好的控件来写1、写好布局文件2、编写自定义 attrs.xml3、编写自定义控件代码4、在activity中使用增加点击事件代码三、各步骤代码
控件名SettingItemView3.1布局文件activity_setting.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:zengmghttp://schemas.android.com/apk/res/com.zengmg.MobileSafeandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewstylestyle/ActivityTitleandroid:textstring/settingTitle /com.zengmg.MobileSafe.view.SettingItemViewandroid:idid/siv_updateandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentzengmg:desc_off自动更新关闭zengmg:desc_on自动更新开启zengmg:title自动更新设置 //LinearLayout 注意需要加入一个自定义的xml声明xmlns:zengmghttp://schemas.android.com/apk/res/com.zengmg.MobileSafe 声明中的 zengmg 是配置属性时的头。 zengmg:desc_off自动更新关闭
zengmg:desc_on自动更新开启
zengmg:title自动更新设置 3.2 自定义 attrs.xml?xml version1.0 encodingutf-8?
resourcesdeclare-styleable nameSettingItemViewattr nametitle formatstring /attr namedesc_on formatstring /attr namedesc_off formatstring //declare-styleable!-- 在D:\ADT\sdk\platforms\android-16\data\res\values\attrs.xml 文件中找 nameTextView。nameSettingItemView 是控件名--/resources 3.3 自定义控件代码 SettingItemView.javapackage 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);mtitleattrs.getAttributeValue(NAMESPACE, title);mDescOnattrs.getAttributeValue(NAMESPACE, desc_on);mDescOffattrs.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 autoUpdatemPref.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();}}});}
}