Android开发学习笔记-自定义组合控件
2014-10-08 23:46
549 查看
为了能让代码能够更多的复用,故使用组合控件。下面是我正在写的项目中用到的方法。
1、先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中。
<?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="68dip" android:id="@+id/aaa" > <TextView android:id="@+id/tv_update_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:text="是否升级" /> <TextView android:layout_below="@id/tv_update_title" android:id="@+id/tv_update_content" android:textSize="15sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:text="停止更新" /> <CheckBox android:checked="false" android:id="@+id/cb_isupdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" /> </RelativeLayout>
2、自定义Java类
package com.frank.mobilesafe.ui; import com.frank.mobilesafe.R; import android.R.bool; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.RelativeLayout; import android.widget.TextView; public class SettingItemView extends RelativeLayout { private CheckBox cb_update; private TextView tv_update_title; private TextView tv_update_content; public SettingItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } private void initView(Context context) { // TODO Auto-generated method stub View.inflate(context, R.layout.setting_item_view, this); cb_update = (CheckBox) findViewById(R.id.cb_isupdate); tv_update_title = (TextView) findViewById(R.id.tv_update_title); tv_update_content = (TextView) findViewById(R.id.tv_update_content); } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public SettingItemView(Context context) { super(context); initView(context); } /** * 检查是否选中 * @return */ public boolean isChecked() { return cb_update.isChecked(); } /** * 设置组合控件的状态 * @param isChecked */ public void SetChecked(boolean isChecked) { cb_update.setChecked(isChecked); } /** * 设置描述信息 * @param isChecked */ public void SetDesc(String text) { tv_update_content.setText(text); } }
3、在主界面中引用
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_maintitle" android:layout_width="match_parent" android:layout_height="55dp" android:background="#8866ff00" android:gravity="center" android:text="设置中心" android:textSize="22sp" /> <com.frank.mobilesafe.ui.SettingItemView android:id="@+id/siv_update" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
4、主界面调用
public class SettingActivity extends Activity { private SettingItemView siv_update; private SharedPreferences sp_update; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); siv_update = (SettingItemView) findViewById(R.id.siv_update); sp_update = getSharedPreferences("config",MODE_PRIVATE); boolean update = sp_update.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_update.edit(); // TODO Auto-generated method stub 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); } } }); } }
5、完成
正常显示相关文章推荐
- Android开发学习笔记-自定义组合控件的过程
- android 开发零起步学习笔记(十七):自定义android用户控件,使用回调函数实现自定义事件
- 学习Android开发-自定义组合控件
- android开发笔记之自定义组合控件
- Android自定义控件之组合控件学习笔记分享
- android开发学习笔记——稍复杂控件 RadioGroup,CheckBox,Toast
- 【Android开发学习笔记】【第四课】基础控件的学习
- Android开发学习之路--UI之自定义布局和控件
- Android开发学习之路--UI之自定义布局和控件
- 【Android开发笔记】自定义表格控件 - TableView
- Android开发学习笔记-自定义TextView属性模版
- 学习 Mars 老师 Android 开发视频教程笔记之01_06 Android 常用控件
- android 开发零起步学习笔记(十):UI控件动态加载机制浅析
- Android自定义控件之开关按钮学习笔记分享
- C#开发学习笔记:向Dev控件库中的GridControl添加自定义复选框
- Android应用开发学习笔记之开发自定义UI组件
- Android游戏开发学习笔记(一):tweened animation自定义动画的实现
- 第九章(3)-开发拥有自定义事件的控件-学习笔记
- android 开发零起步学习笔记(十五):android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- Android学习自定义View(三)——自绘控件和组合控件