输入金额,SpannableStringBuilder,Dialog无主题和透明背景的使用

转载请注明出处: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>

使用代码展示Dialog

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

效果图:

时间: 2025-01-20 15:39:48

输入金额,SpannableStringBuilder,Dialog无主题和透明背景的使用的相关文章

去除安卓自定义Dialog黑色背景,设置无边框,透明

我们在自定义Dialog的时候,往往会希望除去安卓系统定义背景和标题,以便于更好的显示我们自己想要的效果. 其实我们只需要注意几个地方就行了. 1.在Style文件的中定义Dialog的主题 <style name="MyDialog"> <item name="android:windowFrame">@null</item> <item name="android:windowBackground"&

Dialog 样式 主题 标题 背景 使用【总结】

最重要的是这两行代码 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题,需在setContentView之前设置 dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//设置Dialog背景透明效果 MainActivity public class MainActivity extends ListActi

QML之窗口(无边框、透明及拖拽)

1.无边框 Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView. 无边框窗口代码如下: QQuickView viwer; //QQuickView继承自QWindow而不是QWidget viwer.setFlags(Qt::FramelessWindowHint); 2.窗口透明 setOpacity可设置整个窗口(包括控件)的透明度,而背景透明则应使用setColor //设置窗口颜色,以下为透明,在viwer.setSource()之前使用

常见的公共函数封装方法(密码强度、手机号验证、邮箱验证、输入金额验证)

//密码复杂度公共函数封装(邮箱,手机号) this.PasswordStrength = function(password) { var rule = Auto517.config.passwordRule.rule; var min = Auto517.config.passwordRule.min; var max = Auto517.config.passwordRule.max; if(rule == 0 && eval('/^[0-9]{' + min + ',' + max

Qt 无边框、透明、可移动、的个性窗体案例详解

很多朋友都问透明的效果怎么做,为什么自己做的无边框窗体不可移动,一个个回答的很累,干脆写出来分享下好了. int main(int argc, char *argv[]){ QApplication::setStyle("cleanlooks"); QApplication a(argc, argv); login w; w.setWindowTitle("ClientLogin"); w.setWindowOpacity(1); w.setWindowFlags(

input框只能输入纯数字+格式化输入金额与银行卡JS代码

HTML页面代码示例: <div class="wrap">   <input type="text" id="bankCard" placeholder="输入银行卡号"> </div>   <div class="wrap">   <input type="text" id="moneyNum" placeho

iOS textField输入金额的限制,小数点前9位,后面两位

iOS textField输入金额的限制,小数点前9位,后面两位,如果不加小数点,最大位数是9位,加上小数点,最大位数是12位,超出最大位数可删除 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // 判断是否输入内容,或者用户点击的是键盘的删除按钮 if (![string isEqualT

UITextField 限制输入金额(项目中遇到判断输入金额)

下面这个限制输入金额限制得很死: 1.要求用户输入首位不能为小数点; 2.小数点后不超过两位,小数点无法输入超过一个; 3.如果首位为0,后面仅能输入小数点: 4.输入金额不超过11位. 1 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 2 { 3 if (textField.text.lengt

微信支付v3开发(5) 扫码并输入金额支付

关键字:微信支付 微信支付v3 动态native支付 统一支付 Native支付 prepay_id 作者:方倍工作室 本文介绍微信支付下的扫描二维码并输入自定义金额的支付的开发过程. 注意 微信支付现在分为v2版和v3版,2014年9月10号之前申请的为v2版,之后申请的为v3版.V3版的微信支付没有paySignKey参数.v2的相关介绍请参考方倍工作室的其他文章.本文介绍的为微信支付v3. 一. OAuth2.0授权 JSAPI 支付前需要调用 登录授权接口获取到用户的 Openid .所