2016-09-06 92 views
4

我有一個貨幣符號String和金額double。直到現在我顯示金額是這樣的:如何在TextView中顯示格式化的金額?

amount.setText(currency + " " + amount); 

而在有些地方我有2個TextView S IN之間,以顯示與填充量:

currency.setText(currency); 
amount.setText(amount); 

這很好。但是,我將這個代碼移動到與android數據綁定的佈局文件,並有一個簡單的方法來格式化和顯示數量在android?

symbol[space]amount一樣,其中金額應格式化爲2個小數點(如果存在),否則爲整數。例如,100將顯示爲100,而不是100.00,但123.2將首先顯示爲123.20以及貨幣符號($ 123.20,$ 100)。

我知道有幾種方法(資源字符串格式,NumberFormat)我可以做到這一點(我還沒有使用它們中的任何一種,只是聽說過它們),但是最簡單和最乾淨的方法是什麼?

我試圖找到並將所有2個文本視圖轉換爲單個文本視圖,並找到格式化和顯示數量的統一方式。

謝謝。

回答

0

由@ Nowshad的回答啓發,我能做到這一點沒有帶數據綁定庫的外部庫。

每當需要在佈局文件提供幣種和金額:

  <TextView 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:gravity="end" 
       android:textAppearance="@style/TextAppearance.AppCompat.Large" 
       app:amount="@{amount}" 
       app:currency="@{currency}" /> 

,並提供一個結合銜接頭:

@BindingAdapter({"currency", "amount"}) 
    public static void setCurrencyAndAmount(TextView textView, String currency, double amount) { 
     SpannableString spannableString = new SpannableString(currency + " " + 
       new DecimalFormat("#.##").format(amount)); 

     spannableString.setSpan(new RelativeSizeSpan(0.6f), 0, 1, 0); 

     textView.setText(spannableString); 
    } 

簡單高效。

1

如果您使用amount.setText(currency + " " + amount);這比每次使用text.getText()時都要多,那麼您必須從textView中分出$和空格,以便將實際金額進行凝聚。

在2 TextView的你不必爲的getText()方法可以直接得到量進行任何操作。

使用這2 deciaml位數和0十進制數數...

double price=23.1000; 
int number=(int)((price*100)%100); 
yourTextView.setText(number==0?new DecimalFormat("0").format(price):new DecimalFormat("0.00").format(price)); 

輸出: -

23.1000 -------> 23.10

23.0007 -------> 23

The以上代碼正在工作.....

注意: - 在上面的2,我建議去與2 textView。這很簡單。

+0

不看起來太複雜?這可以很容易地使用新的DecimalFormat(「#。##」)來完成。format(100.0)' – kirtan403

+0

我用條件語句將單行語句......乘以100,模式乘以100,所以我們可以得到它的最後2位數字來檢查是數字是== 0比我們打印0十進制格式其他我們打印2十進制格式... – sushildlh

+0

@ kirtan403如果你想在多個聲明中讓我知道這對你來說是可以理解的? ? – sushildlh

1

我想你需要看看一些圖書館,你會發現一些很好的例子在這裏是我推薦你最好的圖書館貨幣文本數據綁定我使用它它對我非常有用其保存我的代碼行在這裏是MoneyTextView

<org.fabiomsr.moneytextview.MoneyTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="32dp" 
    app:symbol="$" 
    app:symbolGravity="start|top" 
    app:symbolTextSize="30sp" 
    app:symbolMargin="6dp" 
    app:amount="1256.56" 
    app:baseTextSize="54sp" 
    app:decimalDigitsTextSize="30sp" 
    app:decimalMargin="6dp" 
    app:includeDecimalSeparator="false" 
    app:baseTextColor="#FBFFE3"/> 
+0

這看起來非常好,但如果我可以用簡單的文本視圖來做它會更好。 – kirtan403

0
amount.setText(currency + String.format("%.2f", amount)); 
+0

這沒有任何意義。我已經做到了。 – kirtan403

0

如果你正在尋找一些簡單易用的方式來格式化字符串,並將其設置爲TextView的。

對於流行的方法,遵循此Android, Is it possible to format textview like System.out.format("%02d", n);?

如果你要使用數據綁定,你需要設置你自己的綁定方法,因爲原產地的android:文本屬性整數(字符串資源)字符串(文本)

在Android官方的,你可以使用類似的東西 (https://developer.android.com/topic/libraries/data-binding/index.html#expression_language

android:text="@{@string/nameFormat(firstName, lastName)}" 
android:text="@{@plurals/banana(bananaCount)}" 

但是,如果你想在一個更靈活的方式,你需要編寫你自己的綁定方法如下所示。

在你的代碼,

@BindingAdapter(value = {"android:text", "android:formatArgs"}, requireAll = false) 
    public static void bindTextStr(TextView tv, String text, Object[] formatstr) { 
     if (formatstr == null) { 
      tv.setText(text); 
     } else { 
      tv.setText(String.format(text, formatstr)); 
     } 
    } 

在你的佈局XML,

<variable 
      name="normalStr" 
      type="java.lang.String"/> 
     <variable name="data" type="Object[]"/> 
... 
<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{normalStr}" 
     android:formatArgs="@{data}" 
     /> 

在活動代碼,

mBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_main, null, false); 
    mBinding.setNormalStr("Data : 1(%s), 2(%d)"); 
    mBinding.setData(new Object[]{"Hello World",12345}); 
0

也許是有用的:MultiSizeTextView

<com.github.captain_miao.multisizetextview.MultiSizeTextView 
    android:id="@+id/multi_text_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 

    app:beforeText="@string/before_text" 
    app:beforeTextColor="@color/red" 
    app:beforeTextSize="@dimen/text_size_small" 
    app:beforeTextStyle="bold" 

    app:centerText="@string/app_name" 
    app:centerTextColor="@color/gray" 
    app:centerTextSize="@dimen/text_size_big" 

    app:afterText="@string/after_text" 
    app:afterTextColor="@color/blue" 
    app:afterTextSize="@dimen/text_size_small" 
    app:afterTextStyle="bold" 
    /> 

enter image description here

0

做這樣的

int enduser=35000000; 
TextView lblEnduser=(TextView)v.findViewById(R.id.lblEnduser); 
lblEnduser.setText(String.format("%,.0f", Float.valueOf(enduser))); 

輸出:35000000