自定义控件
来源:互联网 发布:unity3d工程师待遇 编辑:程序博客网 时间:2024/04/30 03:58
1. 自定义控件分两类, 继承View做功能扩展, 继承ViewGroup做复合控件
自定义一个button为例, 如何自定义事件
KevinButton.java 控件
package com.example.uitest;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.MotionEvent;import android.widget.Button;public class KevinButton extends Button {private KevinClickListener kevinClickListener;public KevinButton(Context context) {super(context);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.RED);canvas.drawRect(5, 5, 30, 30, paint);}public interface KevinClickListener{public void clickChangeColor();}public void setKevinClickListener(KevinClickListener kevinClickListener){this.kevinClickListener = kevinClickListener;}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_UP:long time = event.getEventTime()-event.getDownTime();if(time >= 2000 ){kevinClickListener.clickChangeColor();System.out.println("Time():"+time );}break;default:break;}return super.onTouchEvent(event);}}SelfButtonActivity.java ; activity
public class SelfButtonActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);final KevinButton bt = new KevinButton(this);bt.setKevinClickListener(new KevinClickListener() {@Overridepublic void clickChangeColor() {Toast.makeText(SelfButtonActivity.this, "clickChangeColor", Toast.LENGTH_LONG).show();bt.setBackgroundColor(Color.GREEN);}});setContentView(bt);}}如果将自定义控件放入到xml layout中的两种方法
如果控件是内部类,则用第2中view, 引用内部类的时候用$
<?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="vertical" > <com.example.uitest.KevinButton /> <view class="com.example.uitest.KevinButton"/></LinearLayout>
0 0
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- 自定义控件
- HDU4841 圆桌问题 【HDU最烂题】
- 软件测试
- The 3n + 1 problem(杭电1032)(暴力求解)
- VS不能将参数从const char[]转换为LPCWSTR的解决办法
- Centos配置SVN服务器
- 自定义控件
- 树上莫队算法
- linux shell 字符串操作(长度,查找,替换)详解
- LinuxC学习之简单C循环看汇编
- 非对称加密的逻辑原理 屡清公钥和私钥的关系
- 身份证
- C# 字符串操作
- Linux系统中增加和删除环境变量PATH路径详解
- lucene字典实现原理