Code example for Editable

Methods: append, clear, length, removeSpan, setSpan

0
		public void afterTextChanged(final Editable s) {
			// workaround for German keyboards 
			final String original = s.toString();
			final String replaced = original.replace(',', '.');
			if (!replaced.equals(original)) {
				s.clear();
				s.append(replaced);
			} 
 
			updateSpans(s);
		} 
 
		public void beforeTextChanged(final CharSequence s, final int start,
				final int count, final int after) {
		} 
 
		public void onTextChanged(final CharSequence s, final int start,
				final int before, final int count) {
			updateAppearance(); 
			if (listener != null)
				listener.changed();
		} 
 
		public void onFocusChange(final View v, final boolean hasFocus) {
			if (!hasFocus) {
				final BigInteger amount = getAmount();
				if (amount != null)
					setAmount(amount);
			} 
		} 
 
		public boolean onEditorAction(final TextView v, final int actionId,
				final KeyEvent event) {
			if (actionId == EditorInfo.IME_ACTION_DONE && listener != null)
				listener.done();
 
			return false; 
		} 
	} 
 
	public CurrencyAmountView(final Context context) {
		super(context);
		init(context);
	} 
 
	public CurrencyAmountView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		init(context);
	} 
 
	private void init(final Context context) {
		final Resources resources = context.getResources();
		significantColor = resources.getColor(R.color.significant);
		lessSignificantColor = resources.getColor(R.color.less_significant);
		errorColor = resources.getColor(R.color.error);
		deleteButtonDrawable = resources
				.getDrawable(R.drawable.ic_input_delete);
	} 
 
	@Override 
	protected void onFinishInflate() { 
		super.onFinishInflate(); 
 
		final Context context = getContext();
 
		final TextViewListener textViewListener = new TextViewListener();
 
		textView = (TextView) getChildAt(0);
		textView.setInputType(InputType.TYPE_CLASS_NUMBER
				| InputType.TYPE_NUMBER_FLAG_DECIMAL);
		textView.addTextChangedListener(textViewListener);
		textView.setOnFocusChangeListener(textViewListener);
		textView.setOnEditorActionListener(textViewListener);
		textView.setHintTextColor(lessSignificantColor);
		setHint(null); 
		setValidateAmount(textView instanceof EditText);
 
		chooseView = new View(context) {
			@Override 
			protected void onMeasure(final int wMeasureSpec,
					final int hMeasureSpec) {
				setMeasuredDimension(textView.getCompoundPaddingRight(),
						textView.getMeasuredHeight());
			} 
		}; 
		final LayoutParams chooseViewParams = new LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		chooseViewParams.gravity = Gravity.RIGHT;
		chooseView.setLayoutParams(chooseViewParams);
		this.addView(chooseView);
 
		setCurrencyCode(Constants.CURRENCY_CODE_BITCOIN); 
 
		updateAppearance(); 
	} 
 
	public void setCurrencyCode(final String currencyCode) {
		if (currencyCode != null) {
			final float textSize = textView.getTextSize();
			currencyCodeDrawable = new CurrencyCodeDrawable(currencyCode,
					textSize * 20f / 24f, lessSignificantColor,
					textSize * 0.37f);
		} else { 
			currencyCodeDrawable = null;
		} 
 
		updateAppearance(); 
	} 
 
	public void setAmountSigned(final boolean amountSigned) {
		this.amountSigned = amountSigned;
	} 
 
	public void setValidateAmount(final boolean validateAmount) {
		this.validateAmount = validateAmount;
	} 
 
	public void setContextButton(final int contextButtonResId,
			final OnClickListener contextButtonClickListener) {
		this.contextButtonDrawable = getContext().getResources().getDrawable(
				contextButtonResId);
		this.contextButtonClickListener = contextButtonClickListener;
 
		updateAppearance(); 
	} 
 
	public void setListener(final Listener listener) {
		this.listener = listener;
	} 
 
	public BigInteger getAmount() {
		if (isValidAmount()) 
			return Utils.toNanoCoins(textView.getText().toString().trim());
		else 
			return null; 
	} 
 
	public void setAmount(final BigInteger amount) {
		if (amount != null)
			textView.setText(amountSigned ? WalletUtils
					.formatValue(amount, Constants.CURRENCY_PLUS_SIGN,
							Constants.CURRENCY_MINUS_SIGN) : WalletUtils 
					.formatValue(amount));
		else 
			textView.setText(null);
	} 
 
	public void setHint(final BigInteger amount) {
		final SpannableStringBuilder hint;
		if (amount != null)
			hint = new SpannableStringBuilder(WalletUtils.formatValue(amount));
		else 
			hint = new SpannableStringBuilder("0.00");
 
		updateSpans(hint);
		textView.setHint(hint);
	} 
 
	@Override 
	public void setEnabled(final boolean enabled) {
		super.setEnabled(enabled);
 
		textView.setEnabled(enabled);
	} 
 
	public void setTextColor(final int color) {
		significantColor = color;
 
		updateAppearance(); 
	} 
 
	private static final Pattern P_SIGNIFICANT = Pattern.compile("^([-+]"
			+ Constants.THIN_SPACE + ")?\\d*(\\.\\d{0,2})?"); 
	private static Object SIGNIFICANT_SPAN = new StyleSpan(Typeface.BOLD);
	private static Object UNSIGNIFICANT_SPAN = new RelativeSizeSpan(0.85f);
 
	private static void updateSpans(final Editable s) {
		s.removeSpan(SIGNIFICANT_SPAN);
		s.removeSpan(UNSIGNIFICANT_SPAN);
 
		final Matcher m = P_SIGNIFICANT.matcher(s);
		if (m.find()) {
			final int pivot = m.group().length();
			s.setSpan(SIGNIFICANT_SPAN, 0, pivot, 0);
			if (s.length() > pivot)
				s.setSpan(UNSIGNIFICANT_SPAN, pivot, s.length(), 0);
		} 
	} 
 
	private boolean isValidAmount() { 
		final String amount = textView.getText().toString().trim();