我将setOnKeyListener添加到我的Edittext来格式化它。
EditText = ₹ 45,000
按下KEY DELETE之前。我想删除每个删除键按下事件的单个数字,直到第一个数字(即" 4")。在第一个数字删除时,它也应该删除"₹&# 34;从edittext使它成为空白。我写了下面的代码,但我必须手动删除₹。
日志文件:
10-02 18:01:08.360 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 45,000
10-02 18:01:08.361 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 45000
10-02 18:01:08.361 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 5
10-02 18:01:08.488 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 45,00
10-02 18:01:08.488 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 4500
10-02 18:01:08.488 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 4
10-02 18:01:25.297 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 4,500
10-02 18:01:25.297 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 4500
10-02 18:01:25.297 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 4
10-02 18:01:25.472 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 4,50
10-02 18:01:25.472 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 450
10-02 18:01:25.472 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 3
10-02 18:01:27.783 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 450
10-02 18:01:27.783 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 450
10-02 18:01:27.783 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 3
10-02 18:01:28.017 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 45
10-02 18:01:28.017 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 45
10-02 18:01:28.017 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 2
10-02 18:01:41.177 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 45
10-02 18:01:41.177 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 45
10-02 18:01:41.177 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 2
10-02 18:01:41.392 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 4
10-02 18:01:41.392 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 4
10-02 18:01:41.392 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 1
10-02 18:01:56.121 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹ 4
10-02 18:01:56.121 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ 4
10-02 18:01:56.121 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 1
10-02 18:01:56.336 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹
10-02 18:03:04.153 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹
10-02 18:03:04.327 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹
10-02 18:03:04.327 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ ₹
10-02 18:03:04.327 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 1
10-02 18:03:04.616 4731-4731/loginscreen.example.com.girviapp D/Before Replace =﹕ ₹
10-02 18:03:04.616 4731-4731/loginscreen.example.com.girviapp D/After Replace =﹕ ₹
10-02 18:03:04.616 4731-4731/loginscreen.example.com.girviapp D/Length =﹕ 1
代码:
mAmt.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
String tmp = mAmt.getText().toString();
if (keyCode == KeyEvent.KEYCODE_DEL)
{
/* do something */
Log.d("Before Replace = ",tmp);
tmp=tmp.replace("\u20B9 ","");
tmp=tmp.replace(" ","");
if (tmp.length() > 0 )
{
tmp=tmp.replace("\u20B9 ","");
tmp = tmp.replace(",", "");
Log.d("After Replace = ", tmp.replace("\u20B9 ",""));
Log.d("Length = ",String.valueOf(tmp.length()));
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
if(tmp.length()>1)
{
DecimalFormat myFormatter = new DecimalFormat("\u20B9 " + "##,##,##,##,###");
mAmt.setText(myFormatter.format(Integer.parseInt(tmp)));
mAmt.setSelection(tmp.length());
}
}
}
mAmt.setSelection(mAmt.getText().length());
return false;
}
});
答案 0 :(得分:1)
尝试以下:
mAmt.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
String tmp = mAmt.getText().toString();
if (keyCode == KeyEvent.KEYCODE_DEL)
{
/* do something */
Log.d("Before Replace = ",tmp);
tmp=tmp.replace("\u20B9 ","");
tmp=tmp.replace(" ","");
if (tmp.length() > 0 )
{
tmp=tmp.replace("\u20B9 ","");
tmp = tmp.replace(",", "");
Log.d("After Replace = ", tmp.replace("\u20B9 ",""));
Log.d("Length = ",String.valueOf(tmp.length()));
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
if(tmp.length()>1)
{
DecimalFormat myFormatter = new DecimalFormat("\u20B9 " + "##,##,##,##,###");
mAmt.setText(myFormatter.format(Integer.parseInt(tmp)));
mAmt.setSelection(tmp.length());
}
}
}
mAmt.setSelection(mAmt.getText().length());
if(mAmt.getText().length()==3){
mAmt.setText(null)
}
return false;
}
});