[参考的原文地址]

http://blog.csdn.net/l1028386804/article/details/47101387

效果图:

实现方式:

一:自定义一个含有EditText和Button的组件

先创建一个buttonext_layout的自定义组件的XML布局文件

<?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="horizontal">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/clearButton"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Clear"/> </LinearLayout>

二:创建自定义控件类ButExt

public class ButExt extends LinearLayout {
private EditText mEditText;
private Button mButton; public ButExt(Context context) {
super(context);
//使用布局资源填充视图
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加载布局文件
mInflater.inflate(R.layout.buttonext_layout, this, true);
mEditText = (EditText) findViewById(R.id.editText);
mButton = (Button) findViewById(R.id.clearButton);
hookupButton(context); } /**
* button处理程序
*/
private void hookupButton(final Context context) {
mButton.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
mEditText.setText("");
Toast.makeText(context, "文本框数据已清除", Toast.LENGTH_SHORT).show();
}
});
}
/**
*给EditText赋值
*/
    public void setText(String txt){
mEditText.setText(txt);
}
}

三:MainActivity类的实现

public class MainActivity extends AppCompatActivity {
private LinearLayout mLinearLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态的添加我们自定义的组件
mLinearLayout = (LinearLayout) findViewById(R.id.main_layout);
for (int i = 0; i < 3; i++) {
imageViewExt editText = new imageViewExt(this);
editText.setText(i+");
mLinearLayout.addView(editText);
}
}
}
												

Android自定义组件的更多相关文章

  1. Android自定义组件之自动换行及宽度自适应View:WordWrapView

    目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view  TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...

  2. Android自定义组件系列【7】——进阶实践(4)

    上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...

  3. Android自定义组件系列【6】——进阶实践(3)

    上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...

  4. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

  5. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  6. Android 自定义组件之如何实现自定义组件

    参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...

  7. Android自定义组件系列【3】——自定义ViewGroup实现侧滑

    有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...

  8. Android 自定义组件,自定义LinearLayout,ListView等样式的组件

    今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...

  9. 自己写的几个android自定义组件

    http://www.see-source.com/androidwidget/list.html 多多指点,尤其是自定义组件的适配问题,希望能有更好的方法

随机推荐

  1. vtk多线程简单测试

    vtkMultithreader is a class that provides support for multithreaded execution using sproc() on an SG ...

  2. python基础知识(四)

    摘要:主要涉及lambda表达式.python内置函数(open文件重点).冒泡排序 一.lambda表达式 适用于创建简单函数,也叫匿名函数, 函数名 = lambda 参数 : 返回值 funct ...

  3. Eclipse 导入外部项目无法识别为web项目并且无法在部署到tomcat下

    uss_web如果没有左上角那个球,tomcat就识别不出来的. 1.进入项目目录,找到.project文件,打开. 2.找到...代码段,加入如下标签内容并保存: <nature>org ...

  4. 使用axis开发web service服务端

    一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4下载A ...

  5. mysqldump数据库同步遇到的问题

    1.同步数据是遇到 没有 lock database权限,报 " mysqldump: Got error: 1044: Access denied for user 'spider_dat ...

  6. 如何基于Azure平台实现MySQL HA(方法论篇)

    我们都知道,相较于传统的数据中心,Pulic cloud也有劣势,比如说数据库的HA,很多熟悉公有云平台的读者都知道,因为出于安全性性考虑以及一些技术条件的限制,很多本地数据中心的mysql HA方法 ...

  7. Easyui 设置datagrid 进入编辑状态,保存结束编辑

    在datagrid中如何实现让一行进入编辑状态,修改数据后,保存信息呢? //点击列表变成文本框,进入可编辑状态 $(function () { var doc = $(document), tabl ...

  8. 第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash

    1. UIImage imageWithContentsOfFile卡顿 [[UIImage alloc] initWithContentsOfFile 卡顿 2.uitableview scroll ...

  9. 一堆LCT板子

    搞了一上午LCT,真是累死了-- 以前总觉得LCT高大上不好学不好打,今天打了几遍感觉还可以嘛= =反正现在的水平应付不太难的LCT题也够用了,就这样好了,接下来专心搞网络流. 话说以前一直YY不出来 ...

  10. 参数名ASCII码从小到大排序(字典序)

    /// <summary> /// Hashtable字典排序 /// </summary> /// <param name="parameters" ...