自定义android用户控件,使用回调函数实现自定义事件

来源:互联网 发布:淘宝做一张海报多少钱 编辑:程序博客网 时间:2024/06/01 09:11
在android软件设计中会用到好多的控件,但系统自带的控件有好多不能够达到需要实现的功能或是控件不够美观。那怎么办呢?

android应为我们提供了好多的控件,我们可以继承某一控件,然后对它进行重写来实现自己的一些功能。或是直接继承View自己定义自己的控件。

下面我来讲一下如何写最简单的自定义控件。


1.创建android工程,取名为ControlsTest,直接下一步下一步一直到完成。


2.在工程中新建一个包,命名为paj.control,然后完成



3.在paj.control包中新建一个类,起名为mycontrol并继承View


并添加,两个参数的构造函数(必须使用两个参数的构造函数,因为自定义的控件需要在XML布局中使用,里面含有属性)



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

mycontrol.java代码:

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


activity_main.xml代码

[html] view plaincopy
  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.     <!-- 显示自定义控件返回的值 -->  
  15.     <TextView android:id="@+id/test"   
  16.         android:layout_width="match_parent"   
  17.         android:layout_height="match_parent"  
  18.         />  
  19.    
  20. </LinearLayout>  



MainActivity.java 代码:

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

1 0