gpt4 book ai didi

没有美元符号的android edittext货币格式

转载 作者:太空狗 更新时间:2023-10-29 14:03:08 25 4
gpt4 key购买 nike

我尝试在我的 edittext 中创建货币格式。我搜索并编写了代码,我可以在我的 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) {

}
})

现在我想在输入中禁用/隐藏 $ 符号。可以隐藏/删除这个符号(我想要没有这个符号的格式)如果有人知道解决方案请帮助我谢谢大家

最佳答案

在您的 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 Activity

<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>

/*这个类已经接受复制过去的值*/

公共(public)类 PayTextWatcher 实现 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;
}

}

结果是:

enter image description here

关于没有美元符号的android edittext货币格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com