转载请注明出处: http://blog.youkuaiyun.com/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.youkuaiyun.com/detail/forwardyzk/8410609
效果图: