学习参考:http://blog.csdn.net/hudashi/article/details/50913257

http://blog.csdn.net/gebitan505/article/details/18264091

http://blog.csdn.net/psuaije/article/details/8662266

http://www.cnblogs.com/yc-755909659/archive/2015/02/10/4283294.html

下面是源代码:代码中添加了一个接口,这个接口用于给自定义控件设置自定义的事件

mycontrol.Java代码:

  1. package paj.control;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.LinearLayout;
  8. //继承LinearLayout
  9. public class mycontrol extends LinearLayout {
  10. /**
  11. * 一定一个接口
  12. */
  13. public interface ICoallBack{
  14. public void onClickButton(String s);
  15. }
  16. /**
  17. * 初始化接口变量
  18. */
  19. ICoallBack icallBack = null;
  20. /**
  21. * 自定义控件的自定义事件
  22. * @param iBack 接口类型
  23. */
  24. public void setonClick(ICoallBack iBack)
  25. {
  26. icallBack = iBack;
  27. }
  28. //////////////////////////////////////////////////////////////////////////////
  29. ////////////////////////////////////////////////////////////////////////////
  30. private Context _Context;
  31. /**
  32. * 两个参数的构造函数(必须使用两个参数的构造函数,因为自定义的控件需要在XML布局中使用,里面含有属性)
  33. * @param context 调用自定义控件的对象
  34. * @param attrs
  35. */
  36. public mycontrol(Context context, AttributeSet attrs) {
  37. super(context, attrs);
  38. _Context = context;
  39. //将自定义的控件添加到主布局
  40. this.addView(CreateLayout());
  41. }
  42. private View CreateLayout(){
  43. //创建一个LainearLayout布局
  44. LinearLayout layout = new LinearLayout(_Context);
  45. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
  46. layout.setOrientation(LinearLayout.VERTICAL);
  47. layout.setLayoutParams(params);
  48. //创建一个文本编辑框
  49. final EditText edit = new EditText(_Context);
  50. LayoutParams editParams = new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
  51. edit.setLayoutParams(editParams);
  52. //创建一个按钮
  53. Button button = new Button(_Context);
  54. LayoutParams btpParams = new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT);
  55. button.setLayoutParams(btpParams);
  56. button.setText("点击获取");
  57. //设置按钮的点击事件
  58. button.setOnClickListener(new OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. // 返回这个自定义控件中计算出的值,使用回调实现
  62. icallBack.onClickButton(edit.getText().toString());
  63. }
  64. });
  65. //文本编辑框和按钮添加到layout布局
  66. layout.addView(edit);
  67. layout.addView(button);
  68. return layout;
  69. }
  70. }

activity_main.xml代码

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. <!-- 定义自己的控件 -->
  3. xmlns:paj_control="http://schemas.android.com/apk/res/paj.control.mycontrol"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. >
  8. <!-- 自定义控件 -->
  9. <paj.control.mycontrol android:id="@+id/mycontrol"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. />
  13. <!-- 显示自定义控件返回的值 -->
  14. <TextView android:id="@+id/test"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. />
  18. </LinearLayout>

MainActivity.java 代码:

    1. package com.example.controlstest;
    2. import paj.control.mycontrol;
    3. import paj.control.mycontrol.ICoallBack;
    4. import android.os.Bundle;
    5. import android.app.Activity;
    6. import android.view.Menu;
    7. import android.widget.TextView;
    8. public class MainActivity extends Activity {
    9. mycontrol _mMycontrol;
    10. TextView textView;
    11. @Override
    12. protected void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. setContentView(R.layout.activity_main);
    15. textView = (TextView)findViewById(R.id.test);
    16. //得到自定义控件
    17. _mMycontrol = (mycontrol)findViewById(R.id.mycontrol);
    18. //实现自定义控件中的setonClick自定义事件
    19. _mMycontrol.setonClick(new ICoallBack() {
    20. @Override
    21. public void onClickButton(String s) {
    22. textView.setText(s);//将自定义控件传递的值显示到文本框内
    23. }
    24. });
    25. }
    26. }

android 自定义控件的更多相关文章

  1. Android自定义控件之自定义ViewGroup实现标签云

    前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...

  2. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  3. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

  4. Android自定义控件之基本原理

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

  5. Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  6. 一起来学习Android自定义控件1

    概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...

  7. [Xamarin.Android] 自定义控件

    [Xamarin.Android] 自定义控件 前言 软件项目开发的过程中,免不了遇到一些无法使用内建控件就能满足的客户需求,例如:时速表.折线图...等等.这时开发人员可以透过自定义控件的方式,为项 ...

  8. android自定义控件实现TextView按下后字体颜色改变

    今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片             第一张是按下后截的图,功能很简单, ...

  9. android 自定义控件(初篇)

    android 自定义控件 在写UI当中很多时候会用到自定义的控件,其实自定义控件就像是定义一个类进行调用就OK了.有些相关的感念可以查看API 下面就用个简单的例子来说明自定义控件: public ...

  10. Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)

    最近一直在学习自定义控件,搜了许多大牛们Blog里分享的小教程,也上GitHub找了一些类似的控件进行学习.发现读起来都不太好懂,就想写这么一篇东西作为学习笔记吧. 一.控件介绍: 进度条在App中非 ...

随机推荐

  1. 厉害了,摩托罗拉发布全球首款支持VR和AR的手机MotoZ

    目前支持谷歌daydream移动VR生态系统的手机型号并不多,除了谷歌自家的Pixel系列外,还有华为推出的mate9系列.如今又到了一位新成员,即升级到Android 7.0后的Moto Z. 更为 ...

  2. 纯JavaScripst的全选、全不选、反选 【转】

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【BZOJ1034】[ZJOI2008]泡泡堂BNB 贪心

    Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛前,对阵 ...

  4. 【BZOJ1008】1008: [HNOI2008]越狱 简单组合数学+快速幂

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...

  5. 通过/etc/rc.local实现开机自动拉起服务

    添加服务到/etc/rc.local 如自动拉起apache服务: /etc/rc.local: #!/bin/sh # # This script will be executed *after* ...

  6. iOS 上线被拒收集

    根据上线被拒的原因 自己 也在慢慢总结  希望对各位有所帮助 1)QQ 微信 等第三方平台 必须要做是否安装应用的检测

  7. Linux的磁盘分区(1)

    分区命名: 1.Linux下的分区命名不同于windows下的命名,对硬盘如IDE硬盘采用类似/dev/hdxy的方式来命名,其中hd表示分区所在的设备类型,如IDE硬盘,x表示硬盘盘号(a为基本主盘 ...

  8. PHP curl 模拟POST 上传文件(含php 5.5后CURLFile)

    <?php /** * Email net.webjoy@gmail.com * author jackluo * 2014.11.21 * */ //* function curl_post( ...

  9. script标签不带属性与带async、defer的区别

    <script> 当页面解析到script标签时,会停止解析并下载对应的脚本,并马上执行,执行完毕后再继续解析页面 <script async> async 在下载脚本的同时不 ...

  10. Log4cplus使用

    Log4cplus使用¶ 1.1 简介 log4cplus是C++编写的开源日志系统,前身是java编写的log4j日志系统.log4cplus具有线程安全.灵活.以及多粒度控制的特点,通过将信息划分 ...