編輯:關於 Android 程式設計
先來看看效果圖:

本文示範如何打造一個類似 TextView、同時能顯示表情圖示與可點擊超連結的自訂控件。首先定義一個用於承載超連結資訊的物件:
/**作為超連接顯示的對象*/ public class LinkInfo implements Comparable{ private String content; private String type; private String id; private boolean bIsFace = false; private boolean bSelected = false; public static final String EMAIL = "Email"; public static final String WEBURL = "WebUrl"; public static final String PHONENUMBER = "PhoneNumber"; private int startIndex; private int endIndex; }
接著,對下面這類混合了表情、電話、Email 與網址的字串進行解析:
String s = "(#大笑)%$%$%3434343434343$%$%youjiancau@163.com$dfsfsfsdffds^15959224872)dfsfdsafsaf@153.cn&&fefrewafrewfjwio(fsfsfsd@tre.com.cn()()()www.baidu.com3242343243www.sohu.com@afjiofjfjaof";
解析的核心邏輯如下:
private static Pattern EMAIL_PATTERN = Patterns.EMAIL_ADDRESS; private static Pattern PHONE_PATTERN = Patterns.PHONE; private static Pattern WEBURL_PATTERN = Patterns.WEB_URL; public static ArrayListparseStr(String strLink) { if(TextUtils.isEmpty(strLink)){ return null; } ArrayList resultList = new ArrayList (); ArrayList infoList = null; try{ infoList = new ArrayList (); Matcher matcher = EMAIL_PATTERN.matcher(strLink); //尋找字符串裡的email的位置 int begin = 0; while(matcher.find()) { int start = matcher.start(); int end = matcher.end(); LinkInfo info = new LinkInfo(); info.setStartIndex(start); info.setEndIndex(end); info.setContent(matcher.group()); info.setType(LinkInfo.EMAIL); infoList.add(info); } Matcher matcher1 = PHONE_PATTERN.matcher(strLink);//尋找字符串裡的號碼的位置 while(matcher1.find()) { int start = matcher1.start(); int end = matcher1.end(); LinkInfo info = new LinkInfo(); info.setStartIndex(start); info.setEndIndex(end); info.setContent(matcher1.group()); info.setType(LinkInfo.PHONENUMBER); infoList.add(info); } //(#大笑) Pattern pattern = Pattern.compile("(\\(#\\S{1,2}\\))"); Matcher matcher2 = pattern.matcher(strLink); while(matcher2.find()) { int start = matcher2.start(); int end = matcher2.end(); System.out.println("====start="+start+"end="+end+"match group="+matcher2.group()); LinkInfo info = new LinkInfo(); info.setStartIndex(start); info.setEndIndex(end); info.setContent(matcher2.group()); info.setFace(true); infoList.add(info); } Collections.sort(infoList); int last = 0; for(int i=0;i Activity 的布局檔如下:
IntroView 提供了載入字串的方法:
public class IntroView extends TextView { private ArrayListtitleList; private int displayWidth = 0; private float displayHeight = 0; private float curLen = 0; private Bitmap starBmp; private Bitmap selectedBmp; private float posX = 0; private float posY = 0; private LinkInfo curInfo;//當前點擊的Link對象 private OnClickLinkListener Listener; private String mFaceType = MSG_FACE_TYPE; public static final String MSG_FACE_TYPE = "msgtype"; public static final String STATUS_FACE_TYPE = "statustype"; public IntroView(Context context) { super(context); } public IntroView(Context context, AttributeSet attrs) { super(context, attrs); } public IntroView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setTitleList(ArrayList titleList){ this.titleList = titleList; displayHeight = 0; requestLayout(); } } 在 Activity 中這樣呼叫:
IntroView news_item_text = (IntroView)findViewById(R.id.news_item_text); //不支持字符串裡有\n String s = "(#大笑)%$%$%3434343434343$%$%youjiancau@163.com$dfsfsfsdffds^15959224872)dfsfdsafsaf@153.cn&&fefrewafrewfjwio(fsfsfsd@tre.com.cn()()()www.baidu.com3242343243www.sohu.com@afjiofjfjaof"; news_item_text.setTitleList(ParseNewsInfoUtil.parseStr(s)); news_item_text.setOnClickLinkListener(this);IntroView 的核心思路是:在 onMeasure 測量寬度與高度時,計算出 titleList 中每個 LinkInfo 的位置資訊以及整個 View 的寬高;接著在 onDraw 時透過迴圈逐一繪製每個 LinkInfo。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); try{ int width = measureWidth(widthMeasureSpec); int height = measureHeight(heightMeasureSpec); setMeasuredDimension(width, height); }catch(Exception ex){ ex.printStackTrace(); } }measureWidth 的實作如下:
private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); int initialWidth = getPaddingLeft() + getPaddingRight(); int width = initialWidth; int maxWidth = 0; TextPaint tempPaint = null; if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { if (tempPaint == null) { tempPaint = new TextPaint(); tempPaint.setStyle(Style.FILL); tempPaint.setAntiAlias(true); tempPaint.setTextSize(getTextSize()); } if (titleList != null && titleList.size() > 0) { maxWidth = specSize; int size = titleList.size(); forLable: for (int i = 0; i < size; i++) { LinkInfo info = titleList.get(i); if (info.isFace()) { Bitmap faceBmp = null; if(mFaceType == MSG_FACE_TYPE) { faceBmp = MessageFaceModel.getInstance().getFaceIcon(info.getContent()); } if (faceBmp != null) { int wSize = faceBmp.getWidth() + 4; if (width + wSize >= maxWidth) { //這裡表示已經計算的寬度大於控件的寬度,那就返回maxWidth就可以了 width = maxWidth; break forLable; } width += wSize; } continue; } String text = info.getContent(); text = text.replaceAll("\n", " "); //因為該控件不支持\n,所以把這個換成空格 if (!TextUtils.isEmpty(text)) { float wSize = tempPaint.measureText(text); if (width + wSize >= maxWidth) { width = maxWidth; break forLable; } width += wSize; } } } result = width; } displayWidth = result; return result; }下一篇將接續說明 measureHeight、onDraw 繪製與 onTouch 點擊事件的部分。
Android 中彈出輸入法導致內容上移的問題排查
前言:某天寫程式時,筆者想達到這樣的效果——點擊某個 EditText 時,只有該區域上移,標題區域保持不動。如下圖所示: 在 AndroidManifest 中,我把
S5P4418 從 SD 卡啟動 Android:分區、fstab 與 bootargs 全流程
一、先確認板子的啟動邏輯 手上的板子是 g4418box,預設從 eMMC 啟動;一旦偵測到插入的 SD 卡,BootROM 會改從 SD 卡啟動。這個優先級由硬體設計決
自訂 View 實戰:九宮格手勢解鎖元件拆解
九宮格手勢解鎖是常見的自訂 View 練習題。它看似只是畫幾個圓點與連線,但實際上涵蓋了狀態管理、座標計算、觸摸事件處理與繪製流程。本文以一個九宮格手勢解鎖 View 為
Android Studio 常見錯誤:BOM 非法字元、res-auto 與 APK 重複檔案
在 Android Studio 開發過程中,常會遇到一些看似不明顯、但會導致編譯或打包失敗的問題。本文整理三種常見錯誤:BOM 造成的非法字元、Gradle 專案中的自