android 自定义控件
学习参考: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代码:
- package paj.control;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- //继承LinearLayout
- public class mycontrol extends LinearLayout {
- /**
- * 一定一个接口
- */
- public interface ICoallBack{
- public void onClickButton(String s);
- }
- /**
- * 初始化接口变量
- */
- ICoallBack icallBack = null;
- /**
- * 自定义控件的自定义事件
- * @param iBack 接口类型
- */
- public void setonClick(ICoallBack iBack)
- {
- icallBack = iBack;
- }
- //////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////
- private Context _Context;
- /**
- * 两个参数的构造函数(必须使用两个参数的构造函数,因为自定义的控件需要在XML布局中使用,里面含有属性)
- * @param context 调用自定义控件的对象
- * @param attrs
- */
- public mycontrol(Context context, AttributeSet attrs) {
- super(context, attrs);
- _Context = context;
- //将自定义的控件添加到主布局
- this.addView(CreateLayout());
- }
- private View CreateLayout(){
- //创建一个LainearLayout布局
- LinearLayout layout = new LinearLayout(_Context);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
- layout.setOrientation(LinearLayout.VERTICAL);
- layout.setLayoutParams(params);
- //创建一个文本编辑框
- final EditText edit = new EditText(_Context);
- LayoutParams editParams = new LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.WRAP_CONTENT);
- edit.setLayoutParams(editParams);
- //创建一个按钮
- Button button = new Button(_Context);
- LayoutParams btpParams = new LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT);
- button.setLayoutParams(btpParams);
- button.setText("点击获取");
- //设置按钮的点击事件
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 返回这个自定义控件中计算出的值,使用回调实现
- icallBack.onClickButton(edit.getText().toString());
- }
- });
- //文本编辑框和按钮添加到layout布局
- layout.addView(edit);
- layout.addView(button);
- return layout;
- }
- }
activity_main.xml代码
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- <!-- 定义自己的控件 -->
- xmlns:paj_control="http://schemas.android.com/apk/res/paj.control.mycontrol"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <!-- 自定义控件 -->
- <paj.control.mycontrol android:id="@+id/mycontrol"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <!-- 显示自定义控件返回的值 -->
- <TextView android:id="@+id/test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- </LinearLayout>
MainActivity.java 代码:
- package com.example.controlstest;
- import paj.control.mycontrol;
- import paj.control.mycontrol.ICoallBack;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- mycontrol _mMycontrol;
- TextView textView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = (TextView)findViewById(R.id.test);
- //得到自定义控件
- _mMycontrol = (mycontrol)findViewById(R.id.mycontrol);
- //实现自定义控件中的setonClick自定义事件
- _mMycontrol.setonClick(new ICoallBack() {
- @Override
- public void onClickButton(String s) {
- textView.setText(s);//将自定义控件传递的值显示到文本框内
- }
- });
- }
- }
android 自定义控件的更多相关文章
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义控件之自定义组合控件
前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android自定义控件之基本原理
前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...
- Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- 一起来学习Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- [Xamarin.Android] 自定义控件
[Xamarin.Android] 自定义控件 前言 软件项目开发的过程中,免不了遇到一些无法使用内建控件就能满足的客户需求,例如:时速表.折线图...等等.这时开发人员可以透过自定义控件的方式,为项 ...
- android自定义控件实现TextView按下后字体颜色改变
今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片 第一张是按下后截的图,功能很简单, ...
- android 自定义控件(初篇)
android 自定义控件 在写UI当中很多时候会用到自定义的控件,其实自定义控件就像是定义一个类进行调用就OK了.有些相关的感念可以查看API 下面就用个简单的例子来说明自定义控件: public ...
- Android自定义控件:进度条的四种实现方式(Progress Wheel的解析)
最近一直在学习自定义控件,搜了许多大牛们Blog里分享的小教程,也上GitHub找了一些类似的控件进行学习.发现读起来都不太好懂,就想写这么一篇东西作为学习笔记吧. 一.控件介绍: 进度条在App中非 ...
随机推荐
- Excel 如何引用某表格中的某一列作为数据有效性验证
1. 首先把数据有效性的列表加入到某个表格中.如下图所示:此表格名称为表5 2. 然后定义名称:公式--定义名称 如下填入信息: 3. 然后再数据有效性验证中输入如下信息即可:
- 用gulp替代fekit构建前端项目
https://segmentfault.com/a/1190000003060016 离开qunar有一个多月了,在离开的时候就决定不再用fekit.做出这个决定并不是因为fekit不好,恰恰相反, ...
- jq prepend() 方法在被选元素的开头(仍位于内部)插入指定内容。 提示:prepend() 和 prependTo() 方法作用相同。差异在于语法:内容和选择器的位置,以及 prependTo() 无法使用函数来插入内容。
<html><head><script type="text/javascript" src="/jquery/jquery.js" ...
- 纯JavaScripst的全选、全不选、反选 【转】
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery中的Ajax几种请求方式
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
- 显式激活数据库( ACTIVATE DATABASE)
某天值班员联系我说,我负责的一套报送系统没有按时生成报文,因为此报警提前量比较大,加上系统经常发生未按时生成报文的事件,也就是没在意,然后不急不慢的到公司,打开系统页面,发现其中一个存储过程跑了将近8 ...
- 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- PHP Pthread多线程 操作
<?php class vote extends Thread { public $res = ''; public $url = array(); public $name = ''; pub ...
- ng-switch 指令
<!--标准用法--> <div ng-switch on="showSecret"> <div ng-switch-when="true& ...
- spring security方法一 自定义数据库表结构
Spring Security默认提供的表结构太过简单了,其实就算默认提供的表结构很复杂,也无法满足所有企业内部对用户信息和权限信息管理的要求.基本上每个企业内部都有一套自己的用户信息管理结构,同时也 ...