Android Studio——简易计算器实现

--------------------------------------------------------------------------------------------------------------------------------------------------------------------
第一次写博客嘿嘿嘿,很激动,记录一下Android开发课第一次试验——简易计算器。

P.S.刚接触Android Studio,纯探索式完成,代码以及内容有些粗糙QAQ

好啦,进入正题
----------------------------------------------------------------------------布局activity_main.xml文件--------------------------------------------------------------
首先是布局activity_main.xml文件,先上代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/msg"
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:autofillHints="" tools:targetApi="o">
    </EditText>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/clc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/clc"></Button>

    <Button
        android:id="@+id/mod"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/per"></Button>

    <Button
        android:id="@+id/div"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/div"></Button>

    <Button
        android:id="@+id/mul"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/mul"></Button>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/seven"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/seven"></Button>

    <Button
        android:id="@+id/eight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/eight"></Button>

    <Button
        android:id="@+id/nine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/nine"></Button>

    <Button
        android:id="@+id/sub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/sub"></Button>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/four"></Button>

    <Button
        android:id="@+id/five"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/five"></Button>

    <Button
        android:id="@+id/six"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/six"></Button>

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        style="?android:attr/borderlessButtonStyle"
        android:text="@string/add"></Button>
</LinearLayout>

<LinearLayout android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false">
    <LinearLayout android:orientation="vertical"
        android:layout_weight="3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="?android:attr/borderlessButtonStyle"
                android:text="@string/one"></Button>

            <Button
                android:id="@+id/two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="?android:attr/borderlessButtonStyle"
                android:text="@string/two"></Button>

            <Button
                android:id="@+id/three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                style="?android:attr/borderlessButtonStyle"
                android:text="@string/three"></Button>
        </LinearLayout>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/zero"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                style="?android:attr/borderlessButtonStyle"
                android:text="@string/zero"></Button>

            <Button
                android:id="@+id/dot"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0px"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/dot"></Button>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/equ"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            style="?android:attr/borderlessButtonStyle"
            android:text="@string/equ"></Button>
    </LinearLayout>
</LinearLayout>

----------------------------------------------------------------------------布局activity_main.xml文件--------------------------------------------------------------
----------------------------------------------------------------------------布局效果图-----------------------------------------------------------------------------
----------------------------------------------------------------------------布局效果图-----------------------------------------------------------------------------
下面是MainActivity.java文件,主要分为四种键分开处理:C键(单个字符清除键),=键(EditText获取所有表达式字符处理键),数字键以及操作符键:
首选是数字键(0~9),其主要实现把本身的value(activity中设置)传给EditText显示出来,由于代码基本一致,就先放一个例子:
------------------------------------------------------------------------------------------------------------------------------------------------------------------
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg.append(zero.getText().toString());
}
});
------------------------------------------------------------------------------------------------------------------------------------------------------------------
其次是操作符键(+-*/.),其实现思路原本和数字键一样,但是不知道为什么append方法不适用于这些操作符,有大佬知道原因的话,烦请告知啊QAQ,于是:
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg.setText(msg.getText()+"+");
}
});
------------------------------------------------------------------------------------------------------------------------------------------------------------------
之后是C(单个字符清除键),其思路是假设EditText中有n个字符,先取出n-1个字符,再把第n位用“ ”代替,代码如下:
clc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count=0;//记录覆盖的空格的个数,由于空格也计入字符串长度中,对下面的每次删去的个数有影响,所以记录并在下面减去
String s = msg.getText().toString();
for(int i=0;i<s.length();i++)
if((s.substring(i,i+1)==""))
count++;
//考虑边界情况
if(count>=s.length()-1)
count = s.length()-1;
s = (String) s.subSequence(0,s.length()-1-count);
s = s.concat("");
msg.setText(s);
}
});
------------------------------------------------------------------------------------------------------------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/pjshhh/p/12376373.html