[Android学习笔记]组合控件的使用
组合控件的使用
开发过程中,多个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学习笔记]组合控件的使用的更多相关文章
- Android学习笔记_11_ListView控件使用
一.界面设计: 1.activity_main.xml文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- android学习笔记七——控件(DatePicker、TimePicker、ProgressBar)
DatePicker.TimePicker ==> DatePicker,用于选择日期 TimePicker,用于选择时间 两者均派生与FrameLayout,两者在FrameLayout的基础 ...
- 十三、Android学习笔记_Andorid控件样式汇总
<!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...
- Android学习笔记_75_Andorid控件样式汇总
<!-- 设置activity为透明 --> <style name="translucent"> <item name="android: ...
- Android学习笔记_57_ExpandableListView控件应用
1.布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- android 自定义空间 组合控件中 TextView 不支持drawableLeft属性
android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...
- android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...
- android 自己定义组合控件
自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...
- Android Studio自定义组合控件
在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
随机推荐
- linux查看接口连接状态
ethtool # ethtool em1 Settings for em1: Supported ports: [ TP ] Supported link modes: 10baseT/Half 1 ...
- STL 源代码剖析 算法 stl_algo.h -- equal_range
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...
- codility上的问题(26) Hydrogenium 2013
题目,挺无聊的.一个裸的最短路.n个点,你住在0,要去买东西,每个点有一个关门时间,问能最早买到食物的时间.有两点注意 (1)有重边 (2) 原图是dicrect连接...但不是有向边,被这个误导了. ...
- Resist the Temptation of the Singleton Pattern
Resist the Temptation of the Singleton Pattern Sam Saariste THE SiNGLETON PATTERN SOLVES MANY OF YOU ...
- PE文件简单介绍
PE(Portable Execute)文件是WIN32下可运行文件遵循的数据格式,也是反汇编调试不可缺少的文件,常见的pe文件有.exe和.dll文件.本文主要介绍pe文件的结构和虚拟内存地址转换到 ...
- telerik 控件 SCRIPT5007: 无法获取未定义或 null 引用的属性“documentElement” (IE 文档模式)
IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有 ...
- How to Create Dump File for Applications
使用WinDBG这个工具,可以在应用程序异常终止或者无响应时获取它的尸体,以用来解剖研究. Creating Dump File 在Vista环境中抓取Dump文件很方便,在task man ...
- delphi高手突破之异常及错误处理
什么是异常?为什么要用它? 所谓“异常”是指一个异常类的对象.Delphi的VCL中,所有异常类都派生于Exception类.该类声明了异常的一般行为.性质.最重要的是,它有一个Message属性可以 ...
- <context-param>与<init-param>的区别与作用(转)
<context-param>的作用:web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件we ...
- python算法之二分查找
说明:大部分代码是在网上找到的,好几个代码思路总结出来的 通常写算法,习惯用C语言写,显得思路清晰.可是假设一旦把思路确定下来,并且又不想打草稿.想高速写下来看看效果,还是python写的比較快.也看 ...