android示例源码,制作一个计算器的app
在myeclipse中配置好ADT和SDK后,新建一个Android Application Project项目即可,只需修改一下两个文件代码,即可开发一个简单的计算器。开发完后,右键安卓项目--》Run--》Run Android Application,如果SDK已经创建好AVD的话,执行完后,就会自动启动AVD,AVD中可以看到你的app了,打开即可使用调试。
需求说明
银行提供整存整取定期储蓄业务,年利率为(一年2.25%,两年2.7%,三年3.24%,大于等于五年3.6%),要求输入本金数目和存款年限,计算到期取款时银行应支付本息。
1.activity_main.xml(app页面)源码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ff.MainActivity" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="本金" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="年限" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
2.MainActivity.java(app后端):
package com.example.ff;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText editText1;
EditText editText2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=(EditText) findViewById(R.id.editText1);
editText2=(EditText) findViewById(R.id.editText2);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
textView=(TextView) findViewById(R.id.textView1);
button1.setOnClickListener(calc);
button2.setOnClickListener(reset);
}
/**
* 计算按钮
*/
private OnClickListener calc = new OnClickListener() {
@Override
public void onClick(View arg0) {
double priamt = Double.parseDouble(editText1.getText().toString());
int year = Integer.parseInt(editText2.getText().toString());
double totamt = 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 = priamt*rate;
totamt = priamt + interest*year;
textView = (TextView) findViewById(R.id.textView3);
textView.setText("本息总额:"+totamt+",其中利息是:"+interest*year);
}
};
private OnClickListener reset = new OnClickListener() {
@Override
public void onClick(View arg0) {
editText1.setText("");
editText2.setText("");
textView.setText("");
}
};
}
本文地址:http://www.yayihouse.com/yayishuwu/chapter/1137