写了个android下的计算器,贴贴源码

来源:互联网 发布:阿里云待遇 编辑:程序博客网 时间:2024/05/02 02:54

下面是activity_main.xml的源码:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="32dp"        android:ems="10"        android:inputType="number" >        <requestFocus />    </EditText>    <Button        android:id="@+id/button1"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/editText1"        android:layout_marginLeft="68dp"        android:layout_marginTop="20dp"        android:text="    +   " />    <Button        android:id="@+id/button2"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/button1"        android:layout_toRightOf="@+id/button1"        android:text="    -    " />    <Button        android:id="@+id/button3"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/button2"        android:layout_toRightOf="@+id/button2"        android:text="    *    " />    <Button        android:id="@+id/button4"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/button3"        android:layout_toRightOf="@+id/button3"        android:text="    /    " />    <Button        android:id="@+id/button5"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/button2"        android:layout_centerHorizontal="true"        android:layout_marginTop="18dp"        android:text="点此计算" />    <EditText        android:id="@+id/textView1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/button5"        android:layout_centerHorizontal="true"        android:layout_marginTop="18dp" /></RelativeLayout>

下面是主体部分的代码:


package com.example.calc;import android.view.View.OnClickListener;import android.os.Bundle;import android.app.Activity;import android.content.DialogInterface;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.support.v4.app.NavUtils;import android.view.*;public class MainActivity extends Activity {private EditText editText1;private Button button1;private Button button2;private Button button3;private Button button4;private Button button5;private EditText editText2;private char oper;private String numOneStr = new String();private String numTwoStr = new String();int numOne;int numTwo;double result;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        editText1 = (EditText)findViewById(R.id.editText1);        button1 = (Button)findViewById(R.id.button1);        button2 = (Button)findViewById(R.id.button2);        button3 = (Button)findViewById(R.id.button3);        button4 = (Button)findViewById(R.id.button4);        button5 = (Button)findViewById(R.id.button5);        editText2 = (EditText)findViewById(R.id.textView1);        button1.setOnClickListener(new button1Listener());        button2.setOnClickListener(new button2Listener());        button3.setOnClickListener(new button3Listener());        button4.setOnClickListener(new button4Listener());        button5.setOnClickListener(new button5Listener());    }        class button1Listener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stuboper = '+';numOneStr = editText1.getText().toString();editText1.setText("");System.out.println("得到的第一个数是" + numOneStr + "进行的运算是" + oper);}        }    class button2Listener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stuboper = '-';numOneStr = editText1.getText().toString();editText1.setText("");System.out.println("得到的第一个数是" + numOneStr + "进行的运算是" + oper);}        }    class button3Listener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stuboper = '*';numOneStr = editText1.getText().toString();editText1.setText("");System.out.println("得到的第一个数是" + numOneStr + "进行的运算是" + oper);}        }    class button4Listener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stuboper = '/';numOneStr = editText1.getText().toString();editText1.setText("");System.out.println("得到的第一个数是" + numOneStr + "进行的运算是" + oper);}      }        class button5Listener implements OnClickListener{public void onClick(View v) {// TODO Auto-generated method stubnumTwoStr = editText1.getText().toString();System.out.println("得到的第二个数是" + numTwoStr);numOne = Integer.parseInt(numOneStr);numTwo = Integer.parseInt(numTwoStr);switch(oper){case '+':result = numOne + numTwo;break;case '-':result = numOne - numTwo;break;case '*':result = numOne * numTwo;break;case '/':if(numTwo != 0)result = numOne / numTwo;else{editText2.setText("发生除零错误");return;}}System.out.println("运算的结果是"  + result);editText1.clearComposingText();editText2.setText(result + "");}        }            @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

还不怎么会在布局文件中实现控件的对齐 。。。一切都是把值写上然后看效果微调 。。。