android 控件众多 , 额 , 具体多少个呢? 貌似有那么几十个吧,也没做个统计,嘿嘿!......
有木有朋友感觉写了那么长时间的android代码,有时候想写点自己的东西的时候却发现自己好像离不开网络耶,什么都需要先到网络上遨游一番才能解决自己的问题。思前想后,个人觉得还是有必要巩固一下自己学习过的东西——想想以前这些东西,自己都写过一遍了,但是折腾一段时间下来都不知道放哪里去了........
好了,废话不多说了,这次准备重新学习一下android的常用控件TextView、EditText、AutoCompleteTextView、Button、CalendarView、CheckBox、Chronometer、CompoundButton、DatePicker、DigitalClock、ExpandableListView、Gallery、GridView、HorizontalScrollView、ImageButton、ImageSwitcher、ImageView、ListPopupWindow、ListView、MultiAutoCompleteTextView、NumberPicker、PopupMenu、PopupWindow、ProgressBar、QuickContactBadge、RadioButton、RadioGroup、RatingBar、RemoteViews、ScrollView、SearchView、SeekBar、SlidingDarwer、Switch、TableHost、TextClock、TextSwitcher、TimePicker、Toast、ToggleButton、VideoView、ViewFlipper、ViewSwitcher、ZoomButton等控件。
今天学习控件EditText(这个控件的属性在以前一篇文章中有详细介绍android中EditText详解), 自定义背景可以参看前面两篇文章,这里也不在赘述。 今天学习的是自动控制文本字体大小的EditText。工作中常常有这样的需求——所有的的字串要在一行显示,但是字串的长度确实不确定的,为了不让长的字串超出控件的宽度,此时就需要重新定义字体的大小。
废话不多说,下面一步一步实现该功能(下文中有不清楚的地方请参见点击一步一步学android控件(之一) —— 开始篇链接):
1、在strings.xml文件中加入如下内容
<!-- strings for EditText --> <string name="default_hint_str">something in hint</string> <!-- end -->
2、创建新的布局文件edit_text_detail.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.xy.zt.selfdefinewieget.ui.AutoFontSizeEditText android:id="@+id/show_edit_text" android:layout_width="match_parent" android:layout_height="90dp" android:hint="@string/default_hint_str" android:textColor="@color/button_drawable_focused_center" android:singleLine="true"/></LinearLayout>
3、创建用于显示界面的activity(内容很简单,So easy ^_^) —— WidgetEditTextActivity.java
package com.xy.zt.selfdefinewieget;import android.app.Activity;import android.os.Bundle;public class WidgetEditTextActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_text_detail); }}4、在ViewData中添加如下内容
public static final int EDIT_TEXT_ID = BUTTON_ID +1 ; public static final String EDIT_TEXT_NAME = "EditText";private static final ViewData mEditText = new ViewData(EDIT_TEXT_NAME, EDIT_TEXT_ID); View_Datas.add(mEditText);
ViewData中的最新内容如下:
package com.xy.zt.selfdefinewieget.data;import java.util.ArrayList;final public class ViewData { public final static ArrayList<ViewData> View_Datas = new ArrayList<ViewData>(); public static final int TEXT_VIEW_ID = 90000; public static final String TEXT_VIEW_NAME = "TextView"; public static final int BUTTON_ID = TEXT_VIEW_ID + 1; public static final String BUTTON_NAME = "Button"; public static final int EDIT_TEXT_ID = BUTTON_ID +1 ; public static final String EDIT_TEXT_NAME = "EditText"; private static final ViewData mTextView = new ViewData(TEXT_VIEW_NAME, TEXT_VIEW_ID); private static final ViewData mButton = new ViewData(BUTTON_NAME, BUTTON_ID); private static final ViewData mEditText = new ViewData(EDIT_TEXT_NAME, EDIT_TEXT_ID); public final String mViewName; public final int mViewId; private ViewData(String name, int id) { mViewName = name; mViewId = id; } static { View_Datas.add(mTextView); View_Datas.add(mButton); View_Datas.add(mEditText); }}
5、添加事件响应内容WidgetsAdapter , 在handleItemClicked 函数中添加如下内容:
case ViewData.EDIT_TEXT_ID: intent.setClass(mContext, WidgetEditTextActivity.class); mContext.startActivity(intent); break;
6、现在回来看看我们的布局文件,文中使用了自定义View—— com.xy.zt.selfdefinewieget.ui.AutoFontSizeEditText , 该EditText规定了一个最大字体值和最小字体值。
private float mMaxFontSize = 100; // the maximum text size in the EditTextprivate float mMinFontSize = 2; // the minimum text size in the EditText在需要的时候根据字体内容调整字体大小 refitText(int width, int height) , 请求调整字体大小时 ,调用获取最优字体大小函数
float size = optimazeSize(fontWidth, fontHeight);
下面是该文件的内容
package com.xy.zt.selfdefinewieget.ui;import com.xy.zt.selfdefinewieget.R;import android.content.Context;import android.graphics.Paint;import android.text.TextUtils;import android.util.AttributeSet;import android.util.TypedValue;import android.widget.EditText;public class AutoFontSizeEditText extends EditText { private float mMaxFontSize = 100; // the maximum text size in the EditText private float mMinFontSize = 2; // the minimum text size in the EditText private Paint mTmpPaint = new Paint(); public AutoFontSizeEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public AutoFontSizeEditText(Context context, AttributeSet attrs) { super(context, attrs); } public AutoFontSizeEditText(Context context) { super(context); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw) { refitText(getWidth(), getHeight()); } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); refitText(getWidth(), getHeight()); } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { refitText(getWidth(), getHeight()); super.onTextChanged(text, start, lengthBefore, lengthAfter); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int tarWidth = getMeasuredWidth(); int tarHeight = getMeasuredHeight(); refitText(tarWidth, tarHeight); setMeasuredDimension(tarWidth, tarHeight); } private void refitText(int width, int height) { if (width <= 0) { return; } int fontWidth = width - getPaddingLeft() - getPaddingRight(); int fontHeight = height - getPaddingBottom() - getPaddingTop(); float size = optimazeSize(fontWidth, fontHeight); setTextSize(TypedValue.COMPLEX_UNIT_PX, size); } private float optimazeSize(int fWidth, int fHeight) { float size = 0; float threshhold = 0.5f; float high = mMaxFontSize, low = mMinFontSize; mTmpPaint.set(getPaint()); String str = getText().toString(); if (TextUtils.isEmpty(str)) { str = getContext().getString(R.string.default_hint_str); } while ((high - low) > threshhold) { size = (high + low) / 2; mTmpPaint.setTextSize(size); if (mTmpPaint.measureText(str) >= fWidth || size >= fHeight) { high = size; } else { low = size; } } return low; }}好了本文就到此结束了,有不清楚的地方请留言,谢谢。下个控件 AutoCompleteTextView。
看到有的朋友可以上传演示的动画,有朋友知道是如何使用的还望不吝赐教,感激不尽!