Android EditText with custom font and clear button
来源:互联网 发布:seo中标签优化 编辑:程序博客网 时间:2024/06/07 05:03
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:
0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889public
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
12345678910<
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:
- Android EditText with custom font and clear button
- button with a big font
- Android custom button
- Create a Button with an Image and Text [Android]
- Android---2---TextView、Button、EditText
- Custom Authentication and Encryption with WCF
- Android Custom View---Floating Action Button Menu
- iOS 7: Interactive Pop Gesture With Custom Back Button
- UGUI -Custom Font
- custom font设置打样
- difference between clear data and clear cache ? Android
- Android基础学习 - 简单控件,EditText 、 Button :
- android listView含CheckBox、Button、EditText
- Android学习笔记-EditText&TextView&Button&菜单栏
- Android学习笔记-EditText&TextView&Button&菜单栏
- 2.Android基础:常见控件----->EditText、Button
- EditText,Button
- IOS如何使用custom font
- HDU 2710 Max Factor 素数先打表
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- 汉字转换拼音
- 黑马程序员--java高新技术学习
- 将文件上传到tomcat
- Android EditText with custom font and clear button
- BaaS服务收藏
- 成都传智播客Java基础与加强班
- java.lang.OutOfMemoryError: GC overhead limit exceeded解决
- viewDidLoad,viewWillDisappear, viewWillAppear等区别
- ok6410学习笔记(12.kset学习记录)
- asp.net CheckBoxList 取值与勾选,复选框后台控制前台checkbox选中
- Maven POM入门
- Linux内存管理