制做计算器

首先看要实现的额效果图
html

第一步咱们要实现的是显示器的显示过程java

        1.在activity_main.xml中实现一个编辑框android

<EditText ide

    android:layout_width="fill_parent"函数

    android:layout_height="60dip"布局

    android:id="@+id/et_input"/>code

此时获得的是不能显示编辑框,咱们须要关联一个 android:background="@drawable/white_bg"xml

可是若是此时没有color.xml文件是就会报错htm

咱们能够新建一个drawable文件夹而且在文件夹中建立一个xml的文件,而且root element中选择shap的造型对象

在文件中须要添加corners(为了倒圆角)和 solid(填充实体颜色为白色)

此时应当注意若是value文件夹下没有color.xml的文件时应当建立那么一个文件

drawable下的文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:radius="5dp"/> 
    <solid 
        android:color="@color/white"/>
    <!--  
	<gradient 
	    android:startColor="@color/white"
	    android:endColor="@color/red"
	    />
	<stroke 
	   android:width="1dp"
	   android:color="@color/black" />-->
</shape>

color的文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffffff</color>
    <color name="grey">#ffDEDEDE</color>
    <color name="orange">#ffff9933</color>
    <color name="ashen">#ffcc6633</color>
    <color name="red">#FC0000</color>
    <color name="black">#000000</color>
</resources>

2.此时咱们开县输入的内容能够编辑,但这不符合实际状况 所以咱们应该加入属性

android:editable="false"

3.可是咱们还发现通常的计算器是按靠右的

加入属性 android:gravity="right|bottom"

此时咱们能够获得一个显示框了

二。按钮的实现

1.实现一个布局 观察图片咱们能够得知是一个直线布局

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal"
    android:gravity="center_horizontal">
</linearLayout>

2.在布局下面添加按钮

    <Button 
        android:layout_width="60dp"
        android:layout_height="60dp" 
        android:text="C"
        android:textSize="20sp"
        android:gravity="right|bottom"
        android:id="@+id/btn_clear"/>

其中的android:gravity="right|bottom"是对控件中的文字进行靠哪边的处理

android:layout_marginTop 是向下靠 表示对于据内边框顶部

咱们能够把android布局简单的理解为html中的布局

-------------------------------------------------------------------------

能够说到了这个时候咱们就已经把安卓的前台样式搞定了下面就是制做相对应的事件了

下面咱们转到主文件MainActivity,java中

首先观察图片咱们能够得知  输入狂是对咱们每一次操做的一个点击事件进行关联的

所以第一步咱们要 impletement Onclicklistener接口

接下来就是实例化对象

过程:

  1. 对对象进行声明

    Button btn_0;

  2. 2.对对象进行匹配id

        btn_0=(Button)findviewbyId(R.id.btn_0);

特别注意:在显示控制面板的时候其中比为R.id.et_showview与对象的名称无关

贴上代码

 public class MainActivity extends Activity implements OnClickListener {
// 重写按钮
 Button btn_0;//0数字按钮
 Button btn_1;//1数字按钮
 Button btn_2;//2数字按钮
 Button btn_3;//3数字按钮
 Button btn_4;//4数字按钮
 Button btn_5;//5数字按钮
 Button btn_6;//6数字按钮
 Button btn_7;//7数字按钮
 Button btn_8;//8数字按钮
 Button btn_9;//9数字按钮
 Button btn_point;//点数字按钮
 Button btn_clear;//清除按钮;
 Button btn_del;//删除按钮;
 Button btn_multiply;//乘法按钮;
 Button btn_plus;//加法按钮;
 Button btn_minus;//减法按钮;
 Button btn_divide;//除法按钮;
 Button btn_equle;//等于按钮;
 EditText et_input;//显示输入内容的显示屏
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  btn_1=(Button)findViewById(R.id.btn_1);
  btn_2=(Button)findViewById(R.id.btn_2);
  btn_3=(Button)findViewById(R.id.btn_3);
  btn_4=(Button)findViewById(R.id.btn_4);
  btn_5=(Button)findViewById(R.id.btn_5);
  btn_6=(Button)findViewById(R.id.btn_6);
  btn_7=(Button)findViewById(R.id.btn_7);
  btn_8=(Button)findViewById(R.id.btn_8);
  btn_9=(Button)findViewById(R.id.btn_9);
  btn_point=(Button)findViewById(R.id.btn_point);
  btn_clear=(Button)findViewById(R.id.btn_clear);
  btn_plus=(Button)findViewById(R.id.btn_pluse);
  btn_minus=(Button)findViewById(R.id.btn_minus);
  btn_multiply=(Button)findViewById(R.id.btn_multiply);
  btn_divide=(Button)findViewById(R.id.btn_divide);
  btn_equle=(Button)findViewById(R.id.btn_equal);
  et_input=(EditText)findViewById(R.id.et_showview);

特别记住若是其中又出现错误 有多是在导入的时候发生了错误。

二。实例化完 咱们就要对每一个对象设置监听点击事件

 三。重写点击事件函数

        1.定义一个相应的事件函数

        2.定义一个结果的返回函数

定义一个double类型的r做为结果输出

             1.首先要肯定是否清零 若是清零的话那么下面的函数也不会执行 所以这个时候咱们能够定义一个变量,用布尔类型来进行传参,而且在函数执行的一开始就将函数设置为true .

            2.    获得屏幕的文字将其转换为字符串的形式,因为IndexOF(查找到索引值,即位置) 和substring(用于截取该位置上的全部字母) 是在String下的一个方法

            3.将字符串转换成一个相对应的数字

            4.最后利用多分枝的结构,if。。。else..将检测字符是否类型并做相应的运算(注意被除数被能为0)

            例:if(op.equals="+"){r=arg1+arg2;}

            5.若是没有小数点的话咱们将其强制转化为整型 

相关文章
相关标签/搜索