当前位置:首页 » android文章 » Android自定义控件的三种实现方式

Android自定义控件的三种实现方式

作者:KNN   日期:2020-03-09   分类:android文章
1.组合原生控件
将自己需要的控件组合起来变成一个新控件,如下制作常见的app页面头部.
 新建一个Android项目,创建一个头部布局view_top.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. android:background="#50e7ab"
  7. android:padding="10dp">
  8. <ImageView
  9. android:id="@+id/top_left"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:src="@mipmap/fanhui_bai" />
  13. <TextView
  14. android:id="@+id/top_title"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerHorizontal="true"RelativeLayout
  18. android:layout_centerVertical="true"
  19. android:text="首页"
  20. android:textSize="17sp"
  21. android:textColor="#ffffff" />
  22. <TextView
  23. android:id="@+id/top_right"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="提交"
  27. android:textSize="17sp"
  28. android:textColor="#ffffff"
  29. android:layout_centerVertical="true"
  30. android:layout_alignParentRight="true" />
  31. </RelativeLayout>

下面创建一个TopView继承RelativeLayout

  1. package t.s.com;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.LayoutInflater;
  5. import android.widget.ImageView;
  6. import android.widget.RelativeLayout;
  7. import android.widget.TextView;
  8. /**
  9. * Created by Administrator on 2017/10/19.
  10. */
  11. public class TopView extends RelativeLayout {
  12. // 返回按钮控件
  13. private ImageView top_left;
  14. // 标题Tv
  15. private TextView top_title;
  16. private TextView top_right;
  17. public TopView(Context context) {
  18. super(context);
  19. }
  20. public TopView(Context context, AttributeSet attrs) {
  21. super(context, attrs);
  22. // 加载布局
  23. LayoutInflater.from(context).inflate(R.layout.view_top, this);
  24. // 获取控件
  25. top_left = (ImageView) findViewById(R.id.top_left);
  26. top_title = (TextView) findViewById(R.id.top_title);
  27. top_right = (TextView) findViewById(R.id.top_right);
  28. }
  29. // 为左侧返回按钮添加自定义点击事件
  30. public void setOnclickLeft(OnClickListener listener) {
  31. top_left.setOnClickListener(listener);
  32. }
  33. // 设置标题的方法
  34. public void setTitle(String title) {
  35. top_title.setText(title);
  36. }
  37. // 设置标题的方法
  38. public void setRightTitle(String title) {
  39. top_right.setText(title);
  40. }
  41. }

然后在activity_main.xml中引用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="t.s.com.MainActivity">
  8. <t.s.com.TopView
  9. android:id="@+id/top_view"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content" />
  12. </LinearLayout>

然后再在MainActivity中对控件做操作

  1. package t.s.com;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Toast;
  6. public class MainActivity extends AppCompatActivity {
  7. private TopView topView;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. topView = (TopView) findViewById(R.id.top_view);
  13. topView.setOnclickLeft(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View view) {
  16. Toast.makeText(MainActivity.this, "点击了返回按钮", Toast.LENGTH_SHORT).show();
  17. }
  18. });
  19. topView.setRightTitle("设置");
  20. topView.setTitle("首页");
  21. }
  22. }
运行效果


2.自己绘制控件

熟悉view的绘制原理
1.measure用来测量View的宽和高。
2.layout用来确定View在父容器中放置的位置。
3.draw用来将view绘制在屏幕上

创建一个类CustomView继承View,实现点击事件接口OnClickListener
  1. package t.s.com;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. /**
  10. * Created by Administrator on 2017/10/19.
  11. */
  12. public class CustomView extends View implements View.OnClickListener {
  13. // 定义画笔
  14. private Paint mPaint;
  15. // 用于获取文字的宽和高
  16. private Rect mRect;
  17. // 计数值,每点击一次本控件,其值增加1
  18. private int mCount=0;
  19. public CustomView(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. // 初始化画笔、Rect
  22. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  23. mRect = new Rect();
  24. // 本控件的点击事件
  25. setOnClickListener(this);
  26. }
  27. @Override
  28. protected void onDraw(Canvas canvas) {
  29. super.onDraw(canvas);
  30. mPaint.setColor(Color.BLACK);
  31. // 绘制一个填充色为蓝色的矩形
  32. canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
  33. mPaint.setColor(Color.WHITE);
  34. mPaint.setTextSize(50);
  35. String text = String.valueOf(mCount);
  36. // 获取文字的宽和高
  37. mPaint.getTextBounds(text, 0, text.length(), mRect);
  38. float textWidth = mRect.width();
  39. float textHeight = mRect.height();
  40. // 绘制字符串
  41. canvas.drawText("点了我"+text+"次", getWidth() / 2 - textWidth / 2, getHeight() / 2
  42. + textHeight / 2, mPaint);
  43. }
  44. @Override
  45. public void onClick(View view) {
  46. mCount++;
  47. invalidate();
  48. }
  49. }




在activity_main.xml中引入该自定义布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="t.s.com.MainActivity">
  9. <t.s.com.TopView
  10. android:id="@+id/top_view"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content" />
  13. <t.s.com.CustomView
  14. android:id="@+id/custom"
  15. android:layout_width="300dp"
  16. android:layout_height="200dp"
  17. android:layout_gravity="center"/>
  18. </LinearLayout>


运行效果图

当然这个自定义控件比较粗糙,实际的要根据业务需求逻辑自己绘制,原理一样.


3.继承原生控件  下面以一个不允许输入表情的EditText作为例子

  1. package t.s.com;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.text.Editable;
  5. import android.text.Selection;
  6. import android.text.Spannable;
  7. import android.text.TextWatcher;
  8. import android.util.AttributeSet;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. /**
  12. * Created by Administrator on 2017/6/5 0005.
  13. */
  14. @SuppressLint("AppCompatCustomView")
  15. public class EmoEditText extends EditText {
  16. //输入表情前的光标位置
  17. private int cursorPos;
  18. //输入表情前EditText中的文本
  19. private String inputAfterText;
  20. //是否重置了EditText的内容
  21. private boolean resetText;
  22. private Context mContext;
  23. public EmoEditText(Context context) {
  24. super(context);
  25. this.mContext = context;
  26. initEditText();
  27. }
  28. public EmoEditText(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. this.mContext = context;
  31. initEditText();
  32. }
  33. public EmoEditText(Context context, AttributeSet attrs, int defStyleAttr) {
  34. super(context, attrs, defStyleAttr);
  35. this.mContext = context;
  36. initEditText();
  37. }
  38. // 初始化edittext 控件
  39. private void initEditText() {
  40. addTextChangedListener(new TextWatcher() {
  41. @Override
  42. public void beforeTextChanged(CharSequence s, int start, int before, int count) {
  43. if (!resetText) {
  44. cursorPos = getSelectionEnd();
  45. // 这里用s.toString()而不直接用s是因为如果用s,
  46. // 那么,inputAfterText和s在内存中指向的是同一个地址,s改变了,
  47. // inputAfterText也就改变了,那么表情过滤就失败了
  48. inputAfterText= s.toString();
  49. }
  50. }
  51. @Override
  52. public void onTextChanged(CharSequence s, int start, int before, int count) {
  53. if (!resetText) {
  54. if (count >= 2) {//表情符号的字符长度最小为2
  55. CharSequence input = s.subSequence(cursorPos, cursorPos + count);
  56. if (containsEmoji(input.toString())) {
  57. resetText = true;
  58. Toast.makeText(mContext, "暂不支持表情评论哦", Toast.LENGTH_SHORT).show();
  59. //是表情符号就将文本还原为输入表情符号之前的内容
  60. setText(inputAfterText);
  61. CharSequence text = getText();
  62. if (text instanceof Spannable) {
  63. Spannable spanText = (Spannable) text;
  64. Selection.setSelection(spanText, text.length());
  65. }
  66. }
  67. }
  68. } else {
  69. resetText = false;
  70. }
  71. }
  72. @Override
  73. public void afterTextChanged(Editable editable) {
  74. }
  75. });
  76. }
  77. /**
  78. * 检测是否有emoji表情
  79. *
  80. * @param source
  81. * @return
  82. */
  83. public static boolean containsEmoji(String source) {
  84. int len = source.length();
  85. for (int i = 0; i < len; i++) {
  86. char codePoint = source.charAt(i);
  87. if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * 判断是否是Emoji
  95. *
  96. * @param codePoint 比较的单个字符
  97. * @return
  98. */
  99. private static boolean isEmojiCharacter(char codePoint) {
  100. return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
  101. (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
  102. ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
  103. && (codePoint <= 0x10FFFF));
  104. }
  105. }

然后在activity_main.xml引入该控件就可以了

  1. <t.s.com.EmoEditText
  2. android:id="@+id/edtext"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" />


学会了原理就可以根据自己的需求逻辑制作控件了.


文章来源网络:点击这里查看源文.