组合控件的使用

开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需
关心内部逻辑,只需获取处理后的结果即可

创建组合控件步骤如下:
1.创建xml布局,定义组合控件的外观
2.定义组合控件类,此类一般继承原生ViewGroup控件,如:LinearLayout
3.在组合控件类中获取对应UI控件,编写内部业务需求

ex:
创建一个带textView的Button

1.xml布局文件textview_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView android:id="@+id/textView"
android:text="textView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/btn"
android:text="button"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

textview_btn.xml

2.TextViewButton.java

public class TextViewButton extends LinearLayout
{
////////////////////////////////////////////////////////////////////////////////////////////// Controls
/**
*TextView控件引用
*/
private TextView textView; /**
* Button控件引用
*/
private Button btn; ////////////////////////////////////////////////////////////////////////////////////////////// Data
private int step; public int getStep() {
return step;
} public void setStep(int step) {
this.step = step;
} //////////////////////////////////////////////////////////////////////////////////////////// Construction
public TextViewButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
getControlsRef(context);
} public TextViewButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
getControlsRef(context);
} public TextViewButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
getControlsRef(context);
} /////////////////////////////////////////////////////////////// Business Logic
private void getControlsRef(Context context)
{
// 获取控件引用
View view = LayoutInflater.from(context).inflate(R.layout.textview_btn,null);
btn = (Button)view.findViewById(R.id.btn);
textView = (TextView)view.findViewById(R.id.textView); // 设置监听
setListeners();
} private void setListeners()
{
btn.setOnClickListener(new View.OnClickListener() { // 内部业务逻辑
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
textView.setText(step+"");
} });
}
}

TextViewButton.java

3.使用组合控件
  a).前端声明
  b).获得引用

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.views.TextViewButton
android:id="@+id/textViewBtn"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.views.TextViewButton>
</RelativeLayout>

activity_main.xml

[Android学习笔记]组合控件的使用的更多相关文章

  1. Android学习笔记_11_ListView控件使用

    一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  2. android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)

    DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...

  3. 十三、Android学习笔记_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  4. Android学习笔记_75_Andorid控件样式汇总

    <!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...

  5. Android学习笔记_57_ExpandableListView控件应用

    1.布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  6. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  7. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  8. android 自己定义组合控件

    自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...

  9. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

随机推荐

  1. Android bitmap序列化

    最近在开发中需要用到图片bitmap的序列化并进行传递,发现bitmap是没有序列化的,下面是自己实现的一个序列化方法,分享下. 以下是通过byte[]来进行序列化的,因为bitmap是没有序列化的, ...

  2. IP数据报首部校验和算法

    当用google搜索IP数据报首部校验和算法的时候,总是看到的是代码,没有看到其过程,于是就有了此文,如有错误请指正.文章省略一点,呵呵   IP/ICMP/IGMP/TCP/UDP等协议的校验和算法 ...

  3. CentOS: make menuconfig error: curses.h: No such file or directory

    the problem  when use centos5 to build kernel or busybox step 1. Centos中关于 ncurses.h:no such file or ...

  4. Java7新特性(一)Coin

    1.语法糖 数字下划线   2.switch语句中的String   3.multicatch   4.final重抛 对比上份代码   5.try-with-resources(TWR) AutoC ...

  5. @Autowired 注释与@Qualifier 注释

    @Service("OrganDaoIbatis") public class OrganDaoIbatis extends BaseDao implements IOrganDa ...

  6. maven项目部署到Repository(Nexus)

    目录[-] (一)下载并安装Nexus (二)配置Nexus Repository 说明: (三)在项目中配置Nexus Repository的信息 (四)发布到Nexus Repository 本文 ...

  7. ASP.NET - 网页重定向 Response.Redirect()

    在网页中使用重定向,意思就是在网站中的某一个页面跳转到另一个页面. Response.Redirect(~/abc.aspx); 使用“~”的作用是可以从任意位置跳转. 如果没有“~”,那么跳转的时候 ...

  8. VC2008下使用OpenSSL 1.0.0g(免编译)

    OpenSSL整个软件包大概可以分成三个主要的功能部分:密码算法库.SSL协议库以及应用程序. 官网:http://www.openssl.org/OpenSSL for Windows:http:/ ...

  9. Cloudera Hadoop 4 实战课程(Hadoop 2.0、集群界面化管理、电商在线查询+日志离线分析)

    课程大纲及内容简介: 每节课约35分钟,共不下40讲 第一章(11讲) ·分布式和传统单机模式 ·Hadoop背景和工作原理 ·Mapreduce工作原理剖析 ·第二代MR--YARN原理剖析 ·Cl ...

  10. BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec   Memory Limit: 512 MB Submit: 418   Solved: 235 [ Submit][ ...