Android EditText with custom font and clear button

本文介绍如何在Android应用中创建一个自定义的EditText组件,该组件具备使用自定义字体和内嵌清除按钮的功能,当输入框内容变化时,清除按钮会相应显示或隐藏。

C’mon, admit it, you’ve always wanted this. Have you ever felt that text clearing in your android application on EditTexts feels… wrong ?

So, what do we want to achive?

Plain and simple, a TextView, based on the same principle as the previous TextView tutorial, with custom fonts (from assets folder) and with a Clear button.

image

Proceed with caution…

  Of course, if you get a request to use custom fonts in you app, this does not apply only to TextViews, but to all items like Buttons, List Items and EditTexts.

First we need to create our own custom control, extending EditText and adding the following:

  • an image resource for the Clear button
  • an TextChangedListener so we only show the Clear button when the EditText contains something
  • method for setting our custom TypeFace
  • TouchListener so we detect if the user taped on the Clear button area.

Our project will contain a few things:

image

  1. Main activity: AndroidTutorialActivity.java
  2. Our custom control: MyEditText
  3. Custom fonts in assets folder
  4. Clear_button_image resource for the button  image
  5. Main.xml as the main layout.

I’ll just post the entire code with comments and I’m sure you’ll understand it in a second:

  
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class MyEditText extends EditText{
 
     //The image we are going to use for the Clear button
     private Drawable imgCloseButton = getResources().getDrawable(R.drawable.clear_button_image);
     
     public MyEditText(Context context) {
         super (context);
         init();
     }
 
     public MyEditText(Context context, AttributeSet attrs, int defStyle) {
         super (context, attrs, defStyle);
         init();
     }
 
     public MyEditText(Context context, AttributeSet attrs) {
         super (context, attrs);
         init();
     }
     
     void init() {
         
         // Set bounds of the Clear button so it will look ok
         imgCloseButton.setBounds( 0 , 0 , imgCloseButton.getIntrinsicWidth(), imgCloseButton.getIntrinsicHeight());
 
         // There may be initial text in the field, so we may need to display the  button
         handleClearButton();
 
         //if the Close image is displayed and the user remove his finger from the button, clear it. Otherwise do nothing
         this .setOnTouchListener( new OnTouchListener() {
             @Override
             public boolean onTouch(View v, MotionEvent event) {
 
                 MyEditText et = MyEditText. this ;
 
                 if (et.getCompoundDrawables()[ 2 ] == null )
                     return false ;
                 
                 if (event.getAction() != MotionEvent.ACTION_UP)
                     return false ;
                 
                 if (event.getX() > et.getWidth() - et.getPaddingRight() - imgCloseButton.getIntrinsicWidth()) {
                     et.setText( "" );
                     MyEditText. this .handleClearButton();
                 }
                 return false ;
             }
         });
 
         //if text changes, take care of the button
         this .addTextChangedListener( new TextWatcher() {
             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {
 
                 MyEditText. this .handleClearButton();
             }
 
             @Override
             public void afterTextChanged(Editable arg0) {
             }
 
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
             }
         });
     }
     
     //intercept Typeface change and set it with our custom font
     public void setTypeface(Typeface tf, int style) {
         if (style == Typeface.BOLD) {
             super .setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Vegur-B 0.602.otf" ));
         } else {
             super .setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Vegur-R 0.602.otf" ));
         }
     }
     
     void handleClearButton() {
         if ( this .getText().toString().equals( "" ))
         {
             // add the clear button
             this .setCompoundDrawables( this .getCompoundDrawables()[ 0 ], this .getCompoundDrawables()[ 1 ], null , this .getCompoundDrawables()[ 3 ]);
         }
         else
         {
             //remove clear button
             this .setCompoundDrawables( this .getCompoundDrawables()[ 0 ], this .getCompoundDrawables()[ 1 ], imgCloseButton, this .getCompoundDrawables()[ 3 ]);
         }
     }
}

Don’t forget to add it in your main layout

 

  
1
2
3
4
5
6
7
8
9
10
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:orientation = "vertical" >
 
     < com.alinberce.app.MyEditText
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"
         android:text = "My cool edit text" />
</ LinearLayout >

Run it and see the result:

 

image

内容概要:本文档系统整合了基于MATLAB/Simulink的风力涡轮机雷达信号仿真、电力系统优化、新能源调控及多领域智能算法应用资源,涵盖风电功率平抑、混合储能协同调频、综合能源系统调度、无人机三维路径规划、电动汽车参与调度、电氢氨耦合系统优化等前沿科研方向。资源包提供大量可复现的Matlab代码、Simulink仿真模型、数据集及配套论文,涉及GWO、PSO、WOA、HHO等多种智能优化算法,以及LSTM、CNN、GRU等深度学习模型在负荷预测、故障诊断、信号处理中的应用,尤其聚焦于风电与雷达信号交互、储能功率分配、虚拟电厂运行、微电网多时间尺度优化等关键技术的仿真实现。; 适合人群:具备一定MATLAB编程基础,从事电力系统、新能源、智能优化、信号处理、雷达仿真、综合能源管理等相关领域的科研人员及研究生;工作1-3年相关方向的工程师。; 使用场景及目标:①开展风力发电系统与雷达信号交互仿真研究;②复现高水平期刊论文中的优化调度、故障诊断、功率预测模型;③进行无人机路径规划、储能系统设计、综合能源系统优化等课题研究与论文撰写;④借助成熟的代码框架快速搭建仿真模型,提升科研效率与工程实践能力。; 阅读建议:建议按主题分类浏览资源列表,优先选择标注“复现”“顶刊”“EI”等高价值项目,结合提供的网盘链接下载完整代码与数据,配合Simulink仿真模型与说明文档进行调试与二次开发,注重算法实现与实际工程问题的深度融合。
日常处理文本内容时,你是否也常常被这些琐碎又烦人的操作消耗大量时间:从网页复制来的文章满是空行和多余空格,要一段段手动清理;从不同渠道收集的文档繁简混杂,格式五花八门,想统一规范却只能逐篇调整;面对数千行的数据,想按行排序、去重、添加行号,却找不到一个趁手的小工具;更别提Base64编解码、URL转义这些开发中高频使用的操作,每次都得打开在线网站反复折腾——这些看似零散的文本处理需求,单独去搜工具费时费力,用Word又杀鸡用牛刀,始终缺少一款能把这些功能聚合在一起、打开即用的桌面工具。而 大飞哥软件自习室——文本批量整理工具 v2.0正是为整合这些高频文本处理需求而生的专业效率工具,它集文件管理、文本清洗、格式转换、查找替换、批量插入、排序打乱、编码解码、实时统计等近三十项实用功能于一身,采用多标签页设计支持同时编辑多个文档,所有操作均支持撤销与重做,让你在写代码、整理素材、编辑文案时再也不用东拼西凑找工具,一个软件搞定全部文本处理需求。 软件的核心功能模块围绕文本处理的完整工作流展开,覆盖了从“粗加工”到“精处理”的各个环节。文本清洗模块提供了行首尾去空格、清除所有空格、删除空行、合并多余空格、去除重复行、清除HTML标签等六项功能,一键将脏乱差的原始文本快速规整为干净可用的素材。文本转换模块则涵盖了繁简互转、标点中英文互转、全半角互转、全部大写/小写/首字母大写等选项,尤其适合需要批量处理多语言或不同格式规范的文本内容。查找替换模块不仅支持普通文本替换,更提供了正则表达式和区分大小写选项,查找、替换、全部替换、选区替换四种模式灵活应对不同场景。批量插入文本模块更是独树一帜——用户可预设两组插入内容,选择插入到当前段首/段尾或所有段首/段尾,四个单选按钮搭配两个输入框,实现八种插入组合,为批量处理结构化文本提供了极大的便利。
随着云原生技术的快速发展,分布式微服务架构已成为企业IT基础设施的主流部署模式,然而传统负载均衡调度算法(如加权最小连接数WLC)采用静态权重配置,难以适应生产环境中流量峰谷波动带来的挑战。本文针对分布式微服务系统的负载均衡调度算法优化这一课题,提出融合历史负载时序预测与实时资源数据计算实例权重的动态权重分配策略(简称DWA),并基于Go语言+Kubernetes技术栈构建了完整系统。在研究方法上,本文首先分析了现有加权最小连接数调度算法的局限性,引入资源预测模块基于ARIMA或LSTM时序预测模型预判流量高峰;随后设计动态权重分配(DWA)策略,融合历史负载时序预测与实时资源数据计算实例权重;最终实现负载感知调度(Latency-aware Load Balancing),融合最小连接数与实时负载双重因素。技术实现采用Go语言作为开发主体,利用gRPC框架实现微服务间高效通信,通过Prometheus监控系统采集Pod节点级别的实时资源指标,并结合Kubernetes容器编排平台完成系统的容器化部署。主要研究成果包括三个方面:第一,提出DWA动态权重分配算法,通过实测验证系统吞吐量提升42%,有效提高了集群资源利用率;第二,实现基于时序预测的调度策略,请求响应时间标准差降低67%,显著改善了流量高峰期的系统稳定性;第三,设计多维度负载指标采集架构,节点负载均衡度达到0.91,实现了更精细化的请求分发。测试场景覆盖4G弱网、WiFi抖动及跨网传输等多种网络条件,验证了系统在复杂网络环境下的适应性。 【课程报告内容】 摘要 第1章 绪论 第2章 相关技术与理论 第3章 系统需求分析 第4章 系统总体设计 第5章 系统详细设计与实现 第6章 系统测试与分析 第7章 总结与展望 参考文献 附件-实现指南
内容概要:本文系统研究了基于去噪概率扩散模型(DDPM)在光伏场景生成中的应用,结合Python代码实现,详细阐述了如何利用扩散模型生成具有高度真实感的光伏发电时间序列数据。该方法通过前向加噪与反向去噪的马尔可夫过程,学习光伏出力的复杂时空特征,有效应对新能源出力的强波动性与不确定性。文中深入解析了DDPM的核心数学原理、基于U-Net的网络架构设计、时间步嵌入机制、损失函数构建及训练优化策略,并展示了其在电力系统规划、优化调度、风险评估等场景下的应用潜力,突出了其在生成高质量、多样化光伏场景方面的显著优势。; 适合人群:具备一定机器学习与深度学习基础,从事新能源电力系统、智能电网、可再生能源预测、场景生成与概率建模等领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决光伏功率固有的间歇性与不确定性导致的建模困难;②为微电网能量管理、储能系统优化配置、电力市场仿真及日前-实时调度提供高保真的输入场景;③提升高比例可再生能源接入下电力系统规划与运行决策的鲁棒性与可靠性; 阅读建议:学习者应结合所提供的完整Python代码,动手实践并深入理解扩散模型的正向扩散与反向生成全过程,重点关注时间步条件输入、U-Net特征提取与跳跃连接、以及噪声预测网络的训练细节,并尝试在真实的光伏历史数据集上进行模型训练与生成效果评估,以充分掌握其技术精髓。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值