轉載請註明出處: http://blog.csdn.net/forwardyzk/article/details/43308573
整理了開發匯中遇到的一些小細節。
1.在EditText中輸入金額,只能輸入正確的金額格式,例如:0.01,0.1,0,123,123.0,123.01
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.test1.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<EditText
android:id="@+id/ed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/show_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="彈出透明的Dialog" />
</LinearLayout>
</LinearLayout>
對EditText的處理
private void initEditText() {
ed = (EditText) findViewById(R.id.ed);
ed.setKeyListener(new DigitsKeyListener(false, true));
ed.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if (justNumber(str) == -2) {
str = str.substring(0, str.length() - 1);
ed.setText(str);
ed.setSelection(str.length());
} else if (justNumber(str) == -1) {
str = "";
ed.setText(str);
ed.setSelection(str.length());
}
}
});
}
/**
* 輸入的只能是數字或者.必須設置EditText鍵盤監聽 DigitsKeyListener numericOnlyListener = new
* DigitsKeyListener(false, true); ed.setKeyListener(numericOnlyListener);
*
* @param str
* @return 0 爲空 1表示最後一個數字爲. 2正常格式的數字 -1表示length=1並且不是數字 -2表示草過了多個小數點
*/
public int justNumber(String str) {
int flag = -2;
if (str.matches("(0|([1-9]+)|(0\\.[0-9]{1,2})|([1-9]+\\.[0-9]{1,2}))")) {
flag = 2;
} else if (TextUtils.isEmpty(str)) {
flag = 0;
} else if (str.matches("(0\\.)|([1-9]+\\.)")) {
flag = 1;
} else {
if (str.length() == 1) {
flag = -1;
}
flag = -2;
}
return flag;
}
添加setKeyListener監聽器,這裏添加的是DigitsKeyListener監聽,並且增加了正則判斷。
對TextView顯示不規則的文字,進行處理,使用的是SpannableStringBuilder。
private void initTextView() {
TextView tv = (TextView) findViewById(R.id.tv);
showContent(tv, "123.56", "%");
}
/**
* @param tv TextView
* @param value 顯示的值
* @param flag 顯示的標記 % ,元 ,$,¥....等
*/
private void showContent(TextView tv, String value, String flag) {
value = value + flag;
SpannableStringBuilder ssb = new SpannableStringBuilder(value);
// 設置字體顏色
ssb.setSpan(
new ForegroundColorSpan(mContext.getResources().getColor(
android.R.color.holo_red_dark)), 0, value.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 設置字體大小
ssb.setSpan(new AbsoluteSizeSpan(40, true), 0, value.length() - 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 設置字體大小
ssb.setSpan(new AbsoluteSizeSpan(18, true), value.length() - 1,
value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 設置加粗樣式
ssb.setSpan(new StyleSpan(Typeface.BOLD), 0, value.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(ssb);
}
對Dialog進行處理,透明背景,無標題,使用xml設置圓形背景,並且有描邊
Dialog加載View的佈局文件
dialog_layout.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:background="@drawable/dialog_bg"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Dialog無主題,完全透明,背景爲圓形,並且描邊"
android:textColor="@android:color/background_dark"
android:textSize="20sp" />
</LinearLayout>
加載View的背景
dialog_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 實心 -->
<solid android:color="@color/white" />
<!-- 描邊 -->
<stroke
android:width="1.5dp"
android:color="@color/shallow_grey" />
<!-- 圓角 -->
<corners android:radius="10dp" />
</shape>
private void showDialog() {
Dialog dialog = new Dialog(mContext, R.style.dialog_tran);
View view = View.inflate(mContext, R.layout.dialog_layout, null);
dialog.addContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
dialog.show();
}
styles.xml
<style name="dialog_tran" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:backgroundDimAmount">0.4</item>
</style>
源碼下載: http://download.csdn.net/detail/forwardyzk/8410609
效果圖: