1. 程式人生 > >技巧篇 之 Just do it (這將會是一個漫長的歷程。。。)

技巧篇 之 Just do it (這將會是一個漫長的歷程。。。)

LZ-Says:要的,始終給不了,是我太過於愚笨,還是我始終沒有那個能力給你想要的?

前言

擼碼,本質還是思維方式,雖不能一時間改變,但卻可以通過時間的積累,達到一個厚積薄發。

那麼,本篇,將記錄 LZ 開發過程中覺得不錯的小方式、小技巧。

期待,未來越來越好的自己。

前方高能,請記好安全帶。

LZ 投個懶,依次按照順序記錄把~

1. 巧用 &#160 ; (使用時刪除中間空格)

話說某天老大說,使用者切換到英文語言下,某個提示中間加個空格。

LZ 一想直接 string.xml 敲個空格不就好了嗎?然而卵,然後 LZ 通過判斷當前語言環境動態設定空格,原以為很完美,But,明遠小哥哥微微一笑,說我改了,你有時間把你那裡刪了吧。LZ 一瞧,TMD 什麼鬼?

string.xml 這麼寫:
<string name="no_read_count">%1$s&#160;messages</string>
引用時,這麼玩:
mTvTest.setText(getString(R.string.no_read_count,10));

效果嘛,如下:

在這裡插入圖片描述

簡單、實用,方便、快捷。

2. 粗暴多用 TextView

TextView,作為幾乎專案必備且出場率達到百分之九十九的一個最佳輔助沒有之一,那麼,有關它,又有什麼粗暴方式?一起拭目以待。

首先,我們來看下面這種效果:

Long Long Ago,我們是直接使用倆個 TextView 實現,如今,我們只需要一個,粗暴的實現,方案如下:

String hintStr = "<html><body><font color='#000'>當前搜尋條件:</font><font color='#fc8549'>heliquan</font></body></html>";
mTvTest.setText(Html.fromHtml(hintStr));

就問你粗暴不粗暴,感謝我大芬兒

3. 校驗是否為漢字

public boolean isChinese(String str) {
    String regEx = "[\u4e00-\u9fa5]";
    Pattern pat = Pattern.compile(regEx);
    Matcher matcher = pat.matcher(str);
    boolean flg = false;
    if (matcher.find())
        flg = true;
    return flg;
}

4. 獲取 URL 中 host

try {
    mHostPath = new URL(url).getHost();
} catch (MalformedURLException e) {
    e.printStackTrace();
}

PS:URL 隸屬於 java 包下。

5. Android + EditText + FragmentDialog + 軟鍵盤自動彈出

contentView.requestFocus();
mDialogFragment.getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

6. 軟鍵盤隱藏

private void requestInputMethod() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}

7. list 去重 但是會根據字母排序

mSearchHistoryList = new ArrayList<>(new TreeSet<>(mSearchHistoryList));

8. List 去重 排序 保持資料原有狀態

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    mSearchHistoryList = mSearchHistoryList.stream().distinct().collect(Collectors.toList());
} else {
    mSearchHistoryList = removeStringListDupli(mSearchHistoryList);
}

// 去重 保持原有狀態
public List<String> removeStringListDupli(List<String> stringList) {
    Set<String> set = new LinkedHashSet<>();
    set.addAll(stringList);
    stringList.clear();
    stringList.addAll(set);
    return stringList;
}

個人公眾號

不定期釋出博文,最近有點忙,感謝老鐵理解,歡迎關注~