您的位置:首页 > 移动开发 > Android开发

Android EditText with custom font and clear button

2013-06-25 15:28 696 查看
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.





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:





Main activity: AndroidTutorialActivity.java
Our custom control: MyEditText
Custom fonts in assets folder
Clear_button_image resource for the button 



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:

Don’t forget to add it in your main layout

 

Run it and see the result:

 



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息