Android——本息计算器
使用的是2.3.3版本。
一、效果图

二、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 程序标题 -->
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_money"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- 存款本金文本框 -->
<EditText
android:id="@+id/txtMoney"
android:hint="@string/hint_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
<!-- 存款年限 -->
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_year"
android:textAppearance="?android:attr/textAppearanceMedium" />
<!-- 存款年限文本框 -->
<EditText
android:id="@+id/txtYear"
android:hint="@string/hint_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<!-- 计算按钮 -->
<Button
android:id="@+id/btnCalc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_calc" />
<!-- 清除按钮 -->
<Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn_clear" />
<!-- 本金总额和利息显示文本 -->
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>三、strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BankActivity!</string>
<string name="app_name">本息计算器</string>
<string name="txt_money">存款本金</string>
<string name="txt_year">存款年限</string>
<string name="btn_calc">计算</string>
<string name="btn_clear">清除</string>
<string name="view_result"></string>
<string name="hint_money">请输入本金</string>
<string name="hint_year">请输入年份</string>
</resources>四、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.e276.bank"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/girl_icon"
android:label="@string/app_name" >
<activity
android:name=".BankActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>五、BankActivity.java
package org.e276.bank;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* 本息计算器
*
* @author miao
*
*/
public class BankActivity extends Activity {
private EditText txtMoney;// 存款金额文本框
private EditText txtYear;// 存款年限文本框
private Button btnCalc;// 计算按钮
private Button btnClear;// 清除按钮
private TextView txtResult;// 显示结果,本息总额
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 得到所有的控件对象
txtMoney = (EditText) super.findViewById(R.id.txtMoney);
txtYear = (EditText) super.findViewById(R.id.txtYear);
btnCalc = (Button) super.findViewById(R.id.btnCalc);
btnClear = (Button) super.findViewById(R.id.btnClear);
txtResult = (TextView) super.findViewById(R.id.txtResult);
// 计算按钮
btnCalc.setOnClickListener(calc);
// 清除按钮
btnClear.setOnClickListener(reset);
}
/**
* 计算按钮
*/
private OnClickListener calc = new OnClickListener() {
public void onClick(View v) {
// 存款本金
double money = Double.parseDouble(txtMoney.getText().toString());
// 存款年限
double year = Double.parseDouble(txtYear.getText().toString());
// 本息总额
double result = 0;
// 利息率
double rate = 0;
// 根据存款年限判断利息率
if (year == 1) {
rate = 0.025;
} else if (year == 2) {
rate = 0.027;
} else if (year >= 3 && year < 5) {
rate = 0.0324;
} else if (year >= 5) {
rate = 0.036;
}
// 计算利息
double interest = money * rate;
result = money + interest * year;
/*
* 调用资源文件里的信息,方便国际化。
* super.getResources().getString(R.string.hello);
* 如果在内部类里调用,BankActivity.this.getResources().getString(R.string.hello);
*/
// 显示本息总额
txtResult.setText("本息总额是:" + String.valueOf(result) + " RMB"
+ "\n其中利息是:" + (interest * year) + " RMB");
}
};
/**
* 清除按钮
*/
private OnClickListener reset = new OnClickListener() {
public void onClick(View v) {
txtMoney.setText("");
txtYear.setText("");
txtResult.setText("");
}
};
}六、demo
Android-Bank.zip
相关推荐
xfcyhades 2020-11-20
Michael 2020-11-03
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28