没有美元符号的android edittext货币格式 [英] android edittext currency format without dollar symbol
本文介绍了没有美元符号的android edittext货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在自己的edittext中创建货币格式.我进行了搜索,并弄断了代码,然后可以在自己的edittext中添加货币格式
I try to create currency format in my edittext.i searched and i wound code and i can add currency format in my edittext
transfer_maney.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
transfer_maney.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100));
current = formatted;
transfer_maney.setText(formatted);
transfer_maney.setSelection(formatted.length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
})
现在我想在输入中禁用/隐藏$符号.可以隐藏/删除该符号(我希望这种格式没有该符号) 如果有人知道解决方案,请帮助我 谢谢大家
now i want to disable/hide $ symbol inside input.it is a possible to hide/delete this symbol(i want this format without this symbol) if anyone knows solution please help me thanks everyone
推荐答案
在Java代码内部:
/* if you want $ inside editbox , put the $ before comma : "%$,.2f" */
final EditText valorTxt = (EditText) viewAccept.findViewById(R.id.valorInput);
final MoneyTextWatcher ptw = new MoneyTextWatcher(valorTxt, "%,.2f");
valorTxt.addTextChangedListener(ptw);
xml活动
<EditText
android:id="@+id/valorInput"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:hint="Value $"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
/*此类已经接受复制过去的值*/
/*this class already accept copy past values */
公共类PayTextWatcher实现TextWatcher
public class PayTextWatcher implements TextWatcher
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import java.text.DecimalFormat;
import java.util.Locale;
public class PayTextWatcher implements TextWatcher
{
public static final String T = "PayTextWatcher";
private final EditText editText;
protected int max_length = Integer.MAX_VALUE;
private String formatType;
private String current = "";
private boolean insertingSelected = false;
private boolean isDeleting;
/**
* @param editText
* @param formatType String formatting style like "%,.2f $"
*/
public PayTextWatcher(EditText editText, String formatType)
{
this.editText = editText;
this.formatType = formatType;
Log.e(T, "::PayTextWatcher:" + "formatType " + formatType);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
Log.i(T, "::beforeTextChanged:" + "CharSequence " + s + " start=" + start + " count=" + count + " after=" +
after);
if (after <= 0 && count > 0)
{
isDeleting = true;
} else
{
isDeleting = false;
}
if (!s.toString().equals(current))
{
editText.removeTextChangedListener(this);
String clean_text = s.toString().replaceAll("[^\\d]", "");
editText.setText(clean_text);
editText.addTextChangedListener(this);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
Log.i(T, "::onTextChanged:" + "CharSequence " + s + " start=" + start + " count=" + count + " before=" +
before);
if (start == 0 && before >= 4)
{
insertingSelected = true;
}
}
@Override
public synchronized void afterTextChanged(Editable s)
{
Log.i(T, "::afterTextChanged:" + "Editable " + s + "; Current " + current);
if (!s.toString().equals(current))
{
editText.removeTextChangedListener(this);
String digits = s.toString();
if (insertingSelected)
{
digits = String.valueOf(toDouble(digits));
}
String formatted_text;
double v_value = 0;
try
{
formatted_text = String.format(new Locale("pt", "BR"), formatType, Double.parseDouble(digits));
} catch (NumberFormatException nfe)
{
v_value = toDouble(digits);
formatted_text = String.format(new Locale("pt", "BR"), formatType, v_value);
}
current = formatted_text;
editText.setText(formatted_text);
editText.setSelection(formatted_text.length());
editText.addTextChangedListener(this);
}
}
private String deleteLastChar(String clean_text)
{
if (clean_text.length() > 0)
{
clean_text = clean_text.substring(0, clean_text.length() - 1);
} else
{
clean_text = "0";
}
return clean_text;
}
/**
* @param str String with special caracters
*
* @return a double value of string
*/
public double toDouble(String str)
{
str = str.replaceAll("[^\\d]", "");
if (str != null && str.length() > 0)
{
double value = Double.parseDouble(str);
String s_value = Double.toString(Math.abs(value / 100));
int integerPlaces = s_value.indexOf('.');
if (integerPlaces > max_length)
{
value = Double.parseDouble(deleteLastChar(str));
}
return value / 100;
} else
{
return 0;
}
}
public int getMax_length()
{
return max_length;
}
public void setMax_length(int max_length)
{
this.max_length = max_length;
}
}
结果是:
这篇关于没有美元符号的android edittext货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文