1.1创建布局文件caculator.xml
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/widget_edittext_dark"
android:gravity="right"/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
android:background="@drawable/textview_bg"
android:textColor="@android:color/white"
android:textSize="20dip"/>
<LinearLayout style="@style/calculator_linear">
<Button
style="@style/calculator_button"
android:id="@+id/clear"
android:text="清除"
/>
<Button
style="@style/calculator_button"
android:id="@+id/back"
android:text="后退"
/>
LinearLayout>
<LinearLayout style="@style/calculator_linear">
<Button
style="@style/calculator_button"
android:id="@+id/button7"
android:text="7"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button8"
android:text="8"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button9"
android:text="9"
/>
<Button
style="@style/calculator_button"
android:id="@+id/division"
android:text="÷"
/>
LinearLayout>
<LinearLayout style="@style/calculator_linear">
<Button
style="@style/calculator_button"
android:id="@+id/button4"
android:text="4"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button5"
android:text="5"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button6"
android:text="6"
/>
<Button
style="@style/calculator_button"
android:id="@+id/multiplication"
android:text="X"
/>
LinearLayout>
<LinearLayout style="@style/calculator_linear">
<Button
style="@style/calculator_button"
android:id="@+id/button1"
android:text="1"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button2"
android:text="2"
/>
<Button
style="@style/calculator_button"
android:id="@+id/button3"
android:text="3"
/>
<Button
style="@style/calculator_button"
android:id="@+id/subtraction"
android:text="-"
/>
LinearLayout>
<LinearLayout style="@style/calculator_linear">
<Button
style="@style/calculator_button"
android:id="@+id/buttonD"
android:text="."
/>
<Button
style="@style/calculator_button"
android:id="@+id/button0"
android:text="0"
/>
<Button
style="@style/calculator_button"
android:id="@+id/equal"
android:text="="
/>
<Button
style="@style/calculator_button"
android:id="@+id/addition"
android:text="+"
/>
LinearLayout>
1.2 创建Activity文件,绑定caculator.xml文件
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
requestWindowFeature(Window.FEATURE_NO_TITLE); //创建一个无标题的界面
布局文件中的数字、加减乘除按钮,同时还需要使用findviewbyid函数绑定布局文件中的按钮,这里展示部分代码,其余类似。
private Button clear,back;
private Button division,multiplication,subtraction,addition,equal;//÷,X,-,+,=
private Button button1,button2,button3,button4,button5,button6,button7,button8,button9,button0;
private Button buttonD;
private TextView textView1,textView2;
private void prepareView() {
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
button3=(Button) findViewById(R.id.button3);
.....
}
计算函数
private void jisuan() {
double tem=Double.parseDouble(tempnum);
if("+".equals(lastSign)){
count+=tem;
}else if("-".equals(lastSign)){
count=count-tem;
}else if("x".equals(lastSign)){
count=count*tem;
}else if("/".equals(lastSign)){
count=count/tem;
}else if("".equals(lastSign)){
count=tem;
}
Log.d("dddd", "计算:"+count+lastSign+tem+"="+count);
// setMyTextView2(count+"");
}
在进行加减乘除时的预处理,这里展示加操作,其他操作类似
//加操作
private void jia() {
if(!isOver){
jisuan(); //在进行加操作前先把之前的结果计算一次
lastSign="+"; //把操作置为“+”
setIsOperationFalse(); //当isOperation为false时表示当前为输入运算符的状态
isOver=true; //当isOver为true时表示操作未完成,还不能进行“=”操作
text+=tempnum+"+"; //保存整个计算过程
tempnum=""; //将临时存放每次输入的数字的属性值置为空,以便于下一次输入数字
setMyTextView1(text);//显示整个计算过程
}
}
等于操作
private void equal() {//等于
if(!isOver){
isOver=true;//等于之后就over
jisuan();
text+=tempnum+"=";
setMyTextView1(text);
setMyTextView2(""+count);
cacu_text = text+count;
clear2();
}
}
后退操作
private void back(){//后退
if(!isOver){
try{
tempnum=tempnum.substring(0, (tempnum.length()-1));
setMyTextView1(text+tempnum);
}catch (Exception e) {
e.printStackTrace();
Log.e("dddd", e.getMessage());
}
}
}
监听函数,监听每一个按钮执行响应操作,这里展示部分,其余的类似
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
addNum("1");
setIsOperationTrue();
break;
.......
...........
2.1新建record.xml布局文件,显示账单记录。
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="记录"/>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="备注"/>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="时间"/>
TableRow>
TableLayout>
在caculator.xml中添加按钮组件
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="导出"
android:id="@+id/output"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看"
android:id="@+id/show"/>
LinearLayout>
2.2在caculatorActivity类中声明并绑定按钮,使用intent进行页面的跳转。其中showInputDialog函数实现弹出对话框效果
Button btn1 = findViewById(R.id.output);
btn1.setOnClickListener(new View.OnClickListener() {
//点击按钮弹出输入备忘录提示框
public void onClick(View v) {
showInputDialog();
}
});
//查看记录
Button btn2 = findViewById(R.id.show);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CalculatorActivity.this, ShowRecord.class);
Bundle bundle=new Bundle();
bundle.putString("flag","0");
i.putExtras(bundle);
startActivity(i);
}
});
}
private void showInputDialog() {
/*@setView 装入一个EditView
*/
final EditText editText = new EditText(CalculatorActivity.this);
AlertDialog.Builder inputDialog =
new AlertDialog.Builder(CalculatorActivity.this);
inputDialog.setTitle("Remark").setView(editText);
inputDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(CalculatorActivity.this, ShowRecord.class);
Bundle bundle=new Bundle();
bundle.putString("flag","1");
bundle.putString("remark",editText.getText().toString());
bundle.putString("cacu_text",cacu_text);
i.putExtras(bundle);
startActivity(i);
/*Toast.makeText(CalculatorActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();*/
}
}).show();
}
2.3新建class文件Outputcacu,创建数据库工具类,插入账单记录
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建数据库
SQLiteDatabase mydb = this.openOrCreateDatabase("myrecord.db",MODE_PRIVATE,null);
}
private void insert(SQLiteDatabase db,String v1,String v2){
//插入数据SQL语句
String sql="insert into record(text,describe) values("+v1+","+v2+")";
//执行SQL语句
db.execSQL(sql);
}
接收和处理备注信息
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase mydb = this.openOrCreateDatabase("myrecord.db",MODE_PRIVATE,null);
setContentView(R.layout.record);
//接收和处理备注信息
Bundle bundle = this.getIntent().getExtras();
String flag = bundle.getString("flag");
if(flag.equals("0")){
}else if(flag.equals("1")){
String remark = bundle.getString("remark");
String cacu_text = bundle.getString("cacu_text");
if(remark!=""&&cacu_text!=""){
//System.out.println("cacu_text:"+cacu_text+","+"remark:"+remark);
insert(mydb,cacu_text,remark);
}
}
}
最后附上原文链接