Android 自定义 TextView 以显示货币
Android Custom TextView to show Currency
我需要为文本视图设置掩码以将其文本转换为特定样式(货币,虽然是我自己的样式:Rls ###,###,###,###)但是我以后需要能够获得它的原始文本。我该怎么办?
============================================= =========
假设我会将此字符串传递给 TextView:2120000
它应该被用户看到:Rls 2,120,000
但是 .getText 方法应该 return: 2120000
============================================= =========
格式化代码如下:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(textTemp));
任何帮助将不胜感激
每次输入字符时,您都必须修改您的edittext。试试这个代码片段
EditText currencyEditText= (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(s));
currencyEditText.setText(prezzo);
}
});
假设您有 class MyParser
:
public class MyParser {
//...
public static String getRawValue(value) {
//Converts value to raw value, for instance, converts Rls 2,120,000 to 2120000
}
public static String getViewValue(value) {
//Converts value to view value, for instance, converts 2120000 to Rls 2,120,000
}
//...
}
现在,让我们再来一个 class,叫做 MyTextView
:
public class MyTextView extends TextView {
public CharSequence getText () {
return MyParser.getRawValue(super.getText());
}
public void setText (CharSequence text, TextView.BufferType type) {
super.setText(MyParser.getViewValue(text), type)
}
}
您可以使用与格式化字符串相同的 DecimalFormat 对象以另一种方式解析它:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
try {
// get int value
int i = decimalFormat.parse(textView.getText().toString()).intValue();
} catch (ParseException e) {
// handle error here
}
正确的方法是这样的:
将自定义 TextView 创建为 Java Class
public class RialTextView extends TextView {
String rawText;
public RialTextView(Context context) {
super(context);
}
public RialTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RialTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
rawText = text.toString();
String prezzo = text.toString();
try {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
prezzo = decimalFormat.format(Integer.parseInt(text.toString()));
}catch (Exception e){}
super.setText(prezzo, type);
}
@Override
public CharSequence getText() {
return rawText;
}
}
并在 XML 而不是 <TextView
标签中
使用这样的东西:
<com...RialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:id="@+id/tv_itemgriditems_Price"
android:layout_below="@+id/tv_itemgriditems_Remain"
android:layout_centerHorizontal="true"
android:textSize="15sp"
/>
用于将 long 格式转换为 rial 格式
public String FormatCost(long cost){
try {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
return decimalFormat.format(Integer.parseInt(cost+""));
}catch (Exception e) {
return cost + "";
}
}
我需要为文本视图设置掩码以将其文本转换为特定样式(货币,虽然是我自己的样式:Rls ###,###,###,###)但是我以后需要能够获得它的原始文本。我该怎么办?
============================================= =========
假设我会将此字符串传递给 TextView:2120000
它应该被用户看到:Rls 2,120,000
但是 .getText 方法应该 return: 2120000
============================================= =========
格式化代码如下:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(textTemp));
任何帮助将不胜感激
每次输入字符时,您都必须修改您的edittext。试试这个代码片段
EditText currencyEditText= (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(s));
currencyEditText.setText(prezzo);
}
});
假设您有 class MyParser
:
public class MyParser {
//...
public static String getRawValue(value) {
//Converts value to raw value, for instance, converts Rls 2,120,000 to 2120000
}
public static String getViewValue(value) {
//Converts value to view value, for instance, converts 2120000 to Rls 2,120,000
}
//...
}
现在,让我们再来一个 class,叫做 MyTextView
:
public class MyTextView extends TextView {
public CharSequence getText () {
return MyParser.getRawValue(super.getText());
}
public void setText (CharSequence text, TextView.BufferType type) {
super.setText(MyParser.getViewValue(text), type)
}
}
您可以使用与格式化字符串相同的 DecimalFormat 对象以另一种方式解析它:
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
try {
// get int value
int i = decimalFormat.parse(textView.getText().toString()).intValue();
} catch (ParseException e) {
// handle error here
}
正确的方法是这样的: 将自定义 TextView 创建为 Java Class
public class RialTextView extends TextView {
String rawText;
public RialTextView(Context context) {
super(context);
}
public RialTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RialTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
rawText = text.toString();
String prezzo = text.toString();
try {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
prezzo = decimalFormat.format(Integer.parseInt(text.toString()));
}catch (Exception e){}
super.setText(prezzo, type);
}
@Override
public CharSequence getText() {
return rawText;
}
}
并在 XML 而不是 <TextView
标签中
使用这样的东西:
<com...RialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price"
android:id="@+id/tv_itemgriditems_Price"
android:layout_below="@+id/tv_itemgriditems_Remain"
android:layout_centerHorizontal="true"
android:textSize="15sp"
/>
用于将 long 格式转换为 rial 格式
public String FormatCost(long cost){
try {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
return decimalFormat.format(Integer.parseInt(cost+""));
}catch (Exception e) {
return cost + "";
}
}