[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的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...
随机推荐
- Action的返回值类型总结
Action的返回值 MVC 中的 ActionResult是其他所有Action返回类型的基类,下面是我总结的返回类型,以相应的帮助方法: 下面是这些方法使用的更详细的例子 一.返回View ...
- Java:Java快速入门
链接地址:http://www.cnblogs.com/happyframework/p/3332243.html 你好,世界! 源代码组织方式 Java程序由package+class组成,pack ...
- EF架构使用随机排序
c#当中,可以用Random类来获取随机数 EF当中,我们写Linq时,抑或是采用Linq的扩展方法时,发现都没有随机排序的方法,这就要求我们自己去扩展了 引用自http://www.cnblogs. ...
- Cppcheck 1.54 C/C++静态代码分析工具
Cppcheck是一个C/C++代码分析工具,只检测那些编译器通常无法检测到的bug类型. 官方上建议让编译器提供尽量多的警告提示:1.使用Visual C++的话,应使用警告等级4 2.使用GC ...
- 测试markdown语法
测试使用markdown 这是无序列表 空调 洗衣机 电脑 这是有序列表 西瓜 哈密瓜 火龙果 下划线bingo 测试 斜体好丑 粗体很赞 测试插入代码 $(document).ready(funct ...
- perl post发送json数据
sub wx_init { #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=- ...
- perl post 请求带参数
my $url='https://wenjinbao.winfae.com/business/dispatch_post.do?action=submitAdminLogin'; my $res ...
- 14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制:
14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制: MySQL 复制工作对于InnoDB 表和对于MyISAM表. 它是可能使用复制的方式 存储引 ...
- commondatastorage.googleapis.com訪问失败高速解决
谷歌更新以后非常多sampleproject下载不了. http://commondatastorage.googleapis.com訪问失败高速解决这个问题. 使用在线代理就可以,随便推荐一个htt ...
- webdynpro MESSGAE
1. 添加辅助类CL_WDR_DEMO_MESSAGES 环境,设计的控件有:输入控件,按钮,每个按钮对应一个事件.分别是下面,然后报消息 TEXT: SUCCESS: method ONACTIO ...