前言
大写数字转换工具的开发与实现。
一、效果展示
二、代码
准备工作与我开发的另一个小工具(长度转换工具)所用一致,详情请看我上一篇文章。
链接: Android开发实用小工具二——长度转换工具
1.样式布局
res/layout/activity_word_figure_conversion.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe9ecf1"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFFFF">
<ImageView
android:id="@+id/iv_back"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="#ff000000"
android:textSize="17sp"
android:text="大写数字转换工具" />
</RelativeLayout>
<TextView
android:id="@+id/tv_input"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#00000000"
android:gravity="right|center"
android:padding="5dp"
android:textColor="#FFFF8800"
android:textSize="35sp"
android:text="0" />
<TextView
android:id="@+id/tv_results"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginRight="10dp"
android:layout_weight="2"
android:background="#00000000"
android:gravity="right|center"
android:padding="5dp"
android:textColor="#FF000000"
android:textSize="30sp"
android:text="零元整" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_module"
android:orientation="horizontal"
android:padding="20dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="7"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="8"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="9"
android:textColor="#FF000000"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="4"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="5"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="6"
android:textColor="#FF000000"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="1"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="2"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="3"
android:textColor="#FF000000"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="2"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="0"
android:textColor="#FF000000"
android:textSize="35sp" />
<Button
android:id="@+id/btn_pt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="."
android:textColor="#FF000000"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/btn_clr"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="C"
android:textColor="#FF0080FF"
android:textSize="35sp" />
<ImageButton
android:id="@+id/iv_del"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:src="@drawable/ic_delete" />
<Button
android:id="@+id/btn_negate"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="6dp"
android:layout_weight="1"
android:background="@drawable/radio_button_selector"
android:padding="10dp"
android:text="+/-"
android:textColor="#FF0080FF"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2.主代码
WordFigureConversionActivity .java :
public class WordFigureConversionActivity extends AppCompatActivity implements View.OnClickListener {
// 汉语中的数字大写
private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
// 汉语中货币单位大写
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元", "拾", "佰", "仟",
"万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
// 特殊字符
private static final String CN_FULL = "整";
private static final String CN_NEGATIVE = "负";
private static final String CN_ZERO_FULL = "零元";
// 金额的精读,默认为2
private static final int MONEY_PRECISION = 2;
private List<Button> buttonList = new ArrayList<>();
ImageButton iv_del;
TextView tv_input;
TextView tv_results;
// 显示的文本内容
private String showText = "0";
// 当前运算结果
private String resultsText = "零元整";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word_figure_conversion);
// 获取控件,并添加点击事件
findViewById(R.id.iv_back).setOnClickListener(this);
buttonList.add(findViewById(R.id.btn_0));
buttonList.add(findViewById(R.id.btn_1));
buttonList.add(findViewById(R.id.btn_2));
buttonList.add(findViewById(R.id.btn_3));
buttonList.add(findViewById(R.id.btn_4));
buttonList.add(findViewById(R.id.btn_5));
buttonList.add(findViewById(R.id.btn_6));
buttonList.add(findViewById(R.id.btn_7));
buttonList.add(findViewById(R.id.btn_8));
buttonList.add(findViewById(R.id.btn_9));
buttonList.add(findViewById(R.id.btn_pt));
buttonList.add(findViewById(R.id.btn_clr));
buttonList.add(findViewById(R.id.btn_negate));
iv_del = findViewById(R.id.iv_del);
tv_input = findViewById(R.id.tv_input);
tv_results = findViewById(R.id.tv_results);
// 给按钮设置的点击事件
for (Button button : buttonList) {
button.setOnClickListener(this);
}
iv_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() != R.id.iv_back && v.getId() != R.id.iv_del && v.getId() != R.id.btn_clr
&& v.getId() != R.id.btn_negate && v.getId() != R.id.btn_pt)
if (showText.indexOf(".") == -1 && showText.length() >= 16)
return;
String inputText = "";
// 如果不是删除按钮和返回按钮
if (v.getId() != R.id.iv_del && v.getId() != R.id.iv_back) {
inputText = ((TextView) v).getText().toString();
}
switch (v.getId()) {
// 点击了返回按钮
case R.id.iv_back:
finish();
break;
// 点击了清除按钮
case R.id.btn_clr:
clear();
break;
// 点击了删除按钮
case R.id.iv_del:
if (Double.parseDouble(showText) <= 0 && showText.length() == 2) {
showText = "0";
operation();
refreshText();
break;
}
delete();
break;
// 点击了取负按钮
case R.id.btn_negate:
if (Double.parseDouble(showText) > 0) {
showText = "-" + showText;
} else if (Double.parseDouble(showText) < 0) {
showText = showText.substring(1);
}
operation();
refreshText();
break;
// 点击了小数点按钮
case R.id.btn_pt:
if (showText.indexOf(".") == -1)
showText = showText + ".";
refreshText();
break;
// 点击了数字按钮
default:
if (showText.indexOf(".") != -1) {
if (showText.substring(showText.indexOf(".")).length() >= 3)
break;
}
if (showText.equals("0")) {
showText = inputText;
} else {
showText = showText + inputText;
}
operation();
refreshText();
break;
}
}
// 清空并初始化
private void clear() {
showText = "0";
resultsText = "零元整";
refreshText();
}
// 刷新文本显示
private void refreshText() {
tv_input.setText(showText);
tv_results.setText(resultsText);
}
// 回退
private void delete() {
if (showText.length() != 0) {
showText = showText.substring(0, showText.length() - 1);
if (showText.length() == 0)
showText = "0";
operation();
refreshText();
}
}
// 运算
private void operation() {
BigDecimal numberOfMoney = new BigDecimal(showText);
StringBuffer sb = new StringBuffer();
// 返回 -1 表示该数小于 0 ,返回 0 表示该数等于 0 ,返回 1 表示该数大于 0
int signum = numberOfMoney.signum();
// 零元的情况
if (signum == 0) {
resultsText = CN_ZERO_FULL + CN_FULL;
return;
}
// 这里会进行金额的四舍五入
long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();
// 得到小数点后两位的值
long scale = number % 100;
int numUnit = 0;
int numIndex = 0;
boolean getZero = false;
// 判断最后两位数,一共有四种情况:00 = 0,01 = 1,10,11
if (!(scale > 0)) {
numIndex = 2;
number /= 100;
getZero = true;
}
if ((scale > 0) && (!(scale % 10 > 0))) {
numIndex = 1;
number /= 10;
getZero = true;
}
int zeroSize = 0;
while (true) {
if (number <= 0) {
break;
}
// 每次获取到最后一个数
numUnit = (int) (number % 10);
if (numUnit > 0) {
if ((numIndex == 9) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
}
if ((numIndex == 13) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
}
if ((numIndex == 17) && (zeroSize >= 3)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[14]);
}
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
getZero = false;
zeroSize = 0;
} else {
++zeroSize;
// "分", "角", "元", "万", "亿", "兆", "京" 位不会出现零
if (numIndex != 0 && numIndex != 1 && numIndex != 2 &&
numIndex != 6 && numIndex != 10 && numIndex != 14) {
if (!(getZero)) {
sb.insert(0, CN_UPPER_NUMBER[numUnit]);
}
}
if (numIndex == 2) {
if (number > 0) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
} else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
}
getZero = true;
}
// 让 number 每次都去掉最后一个数
number /= 10;
++numIndex;
}
// 如果 signum = -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
if (signum == -1) {
sb.insert(0, CN_NEGATIVE);
}
// 除了 0.00 其他数据都要带特殊字符:整
sb.append(CN_FULL);
resultsText = sb.toString();
}
}
总结
以上就是大写数字转换工具的开发与实现的内容。