簡體   English   中英

如何創建帶有左側貨幣和右側值的編輯文本

[英]How do I create an edit text with currency on left and value on right

如何創建帶有左側貨幣和右側值的編輯文本。

這是圖像

在此處輸入圖片說明

貨幣也可以作為編輯文字的一部分嗎? 或貨幣是不同的編輯文字?

我希望它只能是1個編輯文本。

為什么不創建一個水平方向的LinearLayout並在左側對齊TextView,在右側對齊Edit Text?

使用EditText並設置此屬性android:drawableLeft="@mipmap/ic_launcher"

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:hint="Add money"
        android:gravity="center"
        android:padding="3dp"/>

如果您需要動態更改此圖像,請在Java文件中使用此代碼

參數:-左,上,右,下

editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.drawableLeft, 0, 0, 0);

使用SpannableString可以實現此目的。

activity_main.xml文件中。

<EditText
        android:id="@+id/edtCurrency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Test"
        android:textColor="#000"
        android:textSize="26sp" />

在您的MainActivity.java添加它。

 EditText edtCurrency = (EditText) findViewById(R.id.edtCurrency);
 SpannableString spannableString = new SpannableString("USD  123.456");
 spannableString.setSpan(new UsdSpannableSuperScript((float) 1.0), 2, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 edtCurrency.setText(spannableString);

這是UsdSpannableSuperScript.java

public class UsdSpannableSuperScript extends SuperscriptSpan {
    //divide superscript by this number
    protected int fontScale = 2;

    //shift value, 0 to 1.0
    protected float shiftPercentage = 0;

    //doesn't shift
    UsdSpannableSuperScript() {
    }

    //sets the shift percentage
    UsdSpannableSuperScript(float shiftPercentage) {
        if (shiftPercentage > 0.0 && shiftPercentage < 1.0)
            this.shiftPercentage = shiftPercentage;
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        //original ascent
        float ascent = tp.ascent();

        //scale down the font
        tp.setTextSize(tp.getTextSize() / fontScale);

        //get the new font ascent
        float newAscent = tp.getFontMetrics().ascent;

        //move baseline to top of old font, then move down size of new font
        //adjust for errors with shift percentage
        tp.baselineShift += (ascent - ascent * shiftPercentage)
                - (newAscent - newAscent * shiftPercentage);
    }

    @Override
    public void updateMeasureState(TextPaint tp) {
        updateDrawState(tp);
    }
}

這是屏幕

在此處輸入圖片說明

  1. 僅使用一個EditText也可以工作。 為此,您必須相對於prefixCount設置前綴並從edittext獲取值。

  2. 您可以嘗試這種方式。 這是一個更好的方法。

      <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/grey" android:orientation="horizontal" android:padding="10dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="USD" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@null" android:singleLine="true" /> </LinearLayout> 

輸出 :與上面提到的圖像相同。

  1. 使用單個EditText是另一種方法。 您可以具有貨幣“ USD”的圖像,並將drawableLeft設置為edittext。

      <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:drawableLeft="@drawable/dollar_sign" android:drawablePadding="5dp" android:inputType="number" android:padding="10dp" android:singleLine="true" /> 
Currency currency = new Currency(Locale.US)
final String usd = currency.getCurrencyCode(); //returns USD

若要使用,請擴展TextView並手動添加此邏輯,或者通過代碼進行操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM