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代码:

  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. }
上一篇:[转发]ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)


下一篇:java加解密算法--对称加密工作模式