项目需求:
1.两个框框,第一个输入第二个变,第二个输入第一个变(简单?再加需求)
2.输入的是数字(简单?再加需求)
3.小数输入的时候需要格式化:第一位是0,后面输入别的都不行,只能输入小数点;第一位是小数点,直接个成“0.”(简单?再加需求)
4.数字格式化千分位并限制位数,即“###,###,###.00”(简单?目前没需求了)
之前准备
毕竟程序猿,怎么不会copy呢?百度,Google搜完以后发现,要么是只实现千分位,要么只实现需求3,单独拿出他们写的代码组合在一起永远不是最完美的。所以呢,自食其力,边学边做边实现吧!
支持原作
我的千分位做法基本就是抄袭这里的:https://blog.csdn.net/uusad/article/details/7988730
感谢大佬!感谢大佬!感谢大佬!
其他都是废话,直接上代码
- 先是res文件走起
1.布局文件
2.strings文件(容我骚操作一波)
汇率转换
这是一个单纯的汇率转换工具\n\n1.实现两个EditText的联动\n2.实现EditText内容的实时修改格式化(千分位等)\n\nyy:1 CNY = 9.130515 USD
人民币CNY
人民币CNY金额
美元USD
美元USD金额
真心的希望某年某月某日的时候,可以实现这个汇率大小,甚至全球只用我大天国的人民币
- 源代码走起
1.自定义控件一波
这里有个问题,我并不想显示光标出来,并且也不许在输入过程中在数据中间插入数据,所以将光标永远置于数据之后(别看作者名字叫anCheng,其实他是个爷们,我同学贡献出来的注意)
package com.example.caixiaoshu.rate_change;
import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;
/**
* @author: anCheng
* @date: 2018/4/26.
* @desc:
*/
public class LastInputEditText extends AppCompatEditText {
public LastInputEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public LastInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LastInputEditText(Context context) {
super(context);
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
//保证光标始终在最后面
if (selStart == selEnd) {
//防止不能多选
setSelection(getText().length());
}
}
}
2.主代码
网上大多数都是在TextWatcher中的beforeTextChanged中移除另一个EditText的监听,之后在afterTextChanged中再将另一个EditText监听回来。虽然也能实现,但限于我懒的细细去研究一番,直接在获取焦点的时候设置自己的监听,失去焦点的时候移除自己的监听,完美!!!
package com.example.caixiaoshu.rate_change;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private TextView tvCNY, tvUSD;
private LastInputEditText etCNY, etUSD;
private FormatNumTextWatch textWatch1, textWatch2;
private double rate = 9.130515;
private final int beforePot = 7;//小数点前保留7位
private final int afterPot = 2;//小数点后保留2位
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCNY = findViewById(R.id.tv_cny);
tvUSD = findViewById(R.id.tv_usd);
etCNY = findViewById(R.id.et_cny);
etUSD = findViewById(R.id.et_usd);
textWatch1 = new FormatNumTextWatch(etCNY, etUSD, true);
textWatch2 = new FormatNumTextWatch(etUSD, etCNY, false);
etCNY.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
Log.e(TAG, "etCNY获得了焦点");
changeColor(1);
etCNY.setText("");
etUSD.setText("");
etCNY.addTextChangedListener(textWatch1);
} else {
Log.e(TAG, "etCNY失去了焦点");
changeColor(2);
etCNY.removeTextChangedListener(textWatch1);
}
}
});
etUSD.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
Log.e(TAG, "etUSD获得了焦点");
changeColor(2);
etUSD.setText("");
etCNY.setText("");
etUSD.addTextChangedListener(textWatch2);
} else {
Log.e(TAG, "etUSD失去了焦点");
changeColor(1);
etUSD.removeTextChangedListener(textWatch2);
}
}
});
etCNY.requestFocus();
}
/**
* 根据焦点所在位置实时修改选中位置的颜色
*
* @param i 1,表示etCNY获得焦点;2,表示etUSD获得焦点
*/
private void changeColor(int i) {
if (1 == i) {
tvCNY.setTextColor(getResources().getColor(R.color.colorAccent));
etCNY.setTextColor(getResources().getColor(R.color.colorAccent));
tvUSD.setTextColor(getResources().getColor(R.color.colorPrimary));
etUSD.setTextColor(getResources().getColor(R.color.colorPrimary));
} else {
tvUSD.setTextColor(getResources().getColor(R.color.colorAccent));
etUSD.setTextColor(getResources().getColor(R.color.colorAccent));
tvCNY.setTextColor(getResources().getColor(R.color.colorPrimary));
etCNY.setTextColor(getResources().getColor(R.color.colorPrimary));
}
}
/**
* 自定义的TextWatch
*/
public class FormatNumTextWatch implements TextWatcher {
private EditText currentEt;
private EditText changeEt;
private boolean isCNY;
public FormatNumTextWatch(EditText curretnEt, EditText changeEt, boolean isCNY) {
this.currentEt = curretnEt;
this.changeEt = changeEt;
this.isCNY = isCNY;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
// Log.e(TAG, "onTextChanged ==> start = " + start + ", before = " + before + ", count = " + count);
}
@Override
public void afterTextChanged(Editable editable) {
String currentStr = editable.toString().trim().replace(",", "");
if (!TextUtils.equals(temp, currentStr)) {
String changeStr = formatCurrentNum(currentStr);
currentStr = changeStr.replace(",", "");
currentEt.setText(changeStr);
currentEt.setSelection(changeStr.length());
if (!TextUtils.isEmpty(currentStr) && !TextUtils.equals("", currentStr)) {
double current = Double.valueOf(currentStr);
double change;
if (isCNY) {
change = current * rate;
} else {
change = current / rate;
}
changeEt.setText(formatChangeNum(change));
} else {
changeEt.setText("");
}
}
}
}
private String temp = "";
/**
* 格式化当前输入的数据满足一下要求
* 1.第一位是0时,接下来只能接小数点
* 2.第一位是小数点时,直接前面补0
* 3.可以千分位
* 4.整数最大7位,小数点后最大2位
*
* @param value
* @return
*/
private String formatCurrentNum(String value) {
temp = value;
//处理负数
boolean neg = false;
if (value.startsWith("-")) {
value = value.substring(1);
neg = true;
}
//处理数据前面的0
if (value.indexOf('0') == 0 && value.indexOf('.') != 1) {
value = "0";
}
//处理小数点之前
String before = "";
String after = "";
if (value.indexOf('.') != -1) {
before = value.substring(0, value.indexOf('.'));
after = value.substring(value.indexOf('.'));
} else {
before = value;
}
if (before.length() > beforePot) {
before = before.substring(0, beforePot);
}
value = before + after;
//处理小数之后
String tail = null;
if (value.indexOf('.') == 0) {
value = "0" + value;
}
if (value.indexOf('.') != -1) {
tail = value.substring(value.indexOf('.'));
value = value.substring(0, value.indexOf('.'));
}
StringBuilder sb = new StringBuilder(value);
sb.reverse();
for (int i = 3; i < sb.length(); i += 4) {
sb.insert(i, ',');
}
sb.reverse();
if (neg) {
sb.insert(0, '-');
}
if (tail != null) {
if (tail.length() > afterPot + 1) {
tail = tail.substring(0, afterPot + 1);
}
sb.append(tail);
}
return sb.toString();
}
/**
* 格式化数据,涉及到金额的数据不能四合五入,直接省去后面的金额
*
* @param value
* @return
*/
private String formatChangeNum(Double value) {
DecimalFormat formater = new DecimalFormat("#,##0.##");
formater.setRoundingMode(RoundingMode.FLOOR);
String format = formater.format(value);
return format;
}
}