计算器小例子

来源:互联网 发布:如何下载windows 10 编辑:程序博客网 时间:2024/06/05 04:21
主类:
package com.example.calculator;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.TextView;public class MainActivity extends ActionBarActivity {private static MainActivity mainActivity ;private TextView tv;public MainActivity(){mainActivity = this;}@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.showstring);}public void showStirng(String str){tv.setText(str);}public static MainActivity getMainActivity() {return mainActivity;}}
Calculator类
package com.example.calculator;import android.content.Context;import android.content.Intent;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import android.widget.GridLayout;import android.widget.TextView;public class Calculator extends GridLayout {private Button[] button = new Button[12];private String str="";private char sign;public Calculator(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}public Calculator(Context context, AttributeSet attrs) {super(context, attrs);init();}public Calculator(Context context) {super(context);init();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);findViewById(R.id.bt1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="1";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="2";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt3).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="3";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt4).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="/";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt5).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="4";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt6).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="5";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt7).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="6";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt8).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="*";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt9).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="7";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt10).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="8";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt11).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="9";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt12).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="-";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt13).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="0";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt14).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+=".";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt15).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {str+="+";MainActivity.getMainActivity().showStirng(str);}});findViewById(R.id.bt16).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {char []ch = str.toCharArray();double sum = 0,sum2=0,ans;boolean tag = false;for(int i=0;i<str.length();i++){if(tag==false){if(ch[i]>='0'&&ch[i]<='9'){sum = sum*10+(int)(ch[i]-'0');}else{sign = ch[i];tag = true;continue;}}else{sum2 = sum2*10 + (int)(ch[i]-'0');}}if(sign == '+'){ ans = sum+sum2;}else if(sign=='-'){ ans = sum-sum2;}else if(sign=='*'){ ans = sum*sum2;}else{ ans = sum/sum2;}//System.out.println(sum+" "+sum2);MainActivity.getMainActivity().showStirng(ans+"");str="";}});}private void init(){button[0] = (Button) findViewById(R.id.bt1);button[1] = (Button) findViewById(R.id.bt2);button[2] = (Button) findViewById(R.id.bt3);button[3] = (Button) findViewById(R.id.bt4);button[4] = (Button) findViewById(R.id.bt5);button[5] = (Button) findViewById(R.id.bt6);button[6] = (Button) findViewById(R.id.bt7);button[7] = (Button) findViewById(R.id.bt8);button[8] = (Button) findViewById(R.id.bt9);button[9] = (Button) findViewById(R.id.bt10);button[10] = (Button) findViewById(R.id.bt11);button[11] = (Button) findViewById(R.id.bt12);}}

XML 文件

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#44000000">        <LinearLayout        android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="30dp"        android:background="#55000000">                <TextView            android:text="Input Num: "            android:id="@+id/getstring"            android:layout_width="wrap_content"            android:layout_height="wrap_content"/>        <TextView             android:id="@+id/showstring"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="#22000000"/>                </LinearLayout>    <com.example.calculator.Calculator     android:id="@+id/container"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:columnCount="4"    android:rowCount="5"    android:layout_marginLeft="10dp"    android:layout_marginTop="10dp">        <Button        android:id="@+id/bt1"        android:text="1"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt2"        android:text="2"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt3"        android:text="3"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt4"        android:text="/"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt5"        android:text="4"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt6"        android:text="5"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt7"        android:text="6"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt8"        android:text="*"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt9"        android:text="7"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt10"        android:text="8"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt11"        android:text="9"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt12"        android:text="-"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt13"        android:text="0"        android:layout_columnSpan="2"        android:layout_gravity="fill"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt14"        android:text="."        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>     <Button        android:id="@+id/bt15"        android:text="+"        android:layout_rowSpan="2"        android:layout_gravity="fill"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    <Button        android:id="@+id/bt16"        android:text="="        android:layout_columnSpan="3"        android:layout_gravity="fill"        android:layout_marginLeft="10dp"        android:layout_marginTop="10dp"        android:background="#33000000"/>    </com.example.calculator.Calculator></LinearLayout>



0 0