关闭

Android实验三事件

35人阅读 评论(0) 收藏 举报
分类:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="zhku.edu.exp3_1.MainActivity"
    android:orientation="vertical"
    android:background="#000000"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:layout_marginRight="18dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="40sp"
        android:hint="0"
        android:gravity="end"
        android:background="@drawable/white_bg"
        />
</LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_7"
            style="@style/ButtonBaseStyle"
            android:text="7"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_8"
            style="@style/ButtonBaseStyle"
            android:text="8"
            android:background="@drawable/white_bg"
            />
        <Button
            android:id="@+id/btn_9"
            style="@style/ButtonBaseStyle"
            android:text="9"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_clear"
            style="@style/ButtonBaseStyle"
            android:text="C"
            android:background="@drawable/white_bg"/>
        </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_4"
            style="@style/ButtonBaseStyle"
            android:text="4"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_5"
            style="@style/ButtonBaseStyle"
            android:text="5"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_6"
            style="@style/ButtonBaseStyle"
            android:text="6"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_plus"
            style="@style/ButtonBaseStyle"
            android:text="+"
            android:background="@drawable/white_bg"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_1"
            style="@style/ButtonBaseStyle"
            android:text="1"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_2"
            style="@style/ButtonBaseStyle"
            android:text="2"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_3"
            style="@style/ButtonBaseStyle"
            android:text="3"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_minus"
            style="@style/ButtonBaseStyle"
            android:text="-"
            android:background="@drawable/white_bg"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_0"
            style="@style/ButtonBaseStyle"
            android:text="0"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_equal"
            style="@style/ButtonBaseStyle"
            android:text="="
            android:background="@drawable/pink_bg"/>
        <Button
            android:id="@+id/btn_divide"
            style="@style/ButtonBaseStyle"
            android:text="÷"
            android:background="@drawable/white_bg"/>
        <Button
            android:id="@+id/btn_multiply"
            style="@style/ButtonBaseStyle"
            android:text="×"
            android:background="@drawable/white_bg"/>
    </LinearLayout>
</LinearLayout>

style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="ButtonBaseStyle">
        <item name="android:layout_width">50dp</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:layout_margin">10dp</item>
        <item name="android:textSize">30sp</item>
    </style>
    <style name="ButtonStyle" parent="ButtonBaseStyle">
        <item name="android:background">@drawable/white_bg</item>
        <item name="android:layout_weight">1</item>
    </style>

    <style name="ButtonEqualStyle" parent="ButtonBaseStyle">
        <item name="android:background">@drawable/pink_bg</item>
        <item name="android:layout_weight">1</item>
    </style>
</resources>

white_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="5dp"/>
        <solid android:color="#ffffff"/>
</shape>
pink_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="5dp"/>
        <solid android:color="#ee82ee"/>
</shape>

bg_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/white_bg"/>
    <item android:drawable="@drawable/pink_bg"
          android:state_pressed="true"/> <!--表示点击状态时采用粉红色-->
</selector>


MainActivity.java

package zhku.edu.exp3_1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_show;
    private double num1;
    private double num2;
    private String str_oper="+";//运算符
    private StringBuffer str_display=new StringBuffer();
    private String str_result;//结果显示
    private boolean b_add,b_sub,b_mul,b_div;//运算符开关控制
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_show=(TextView)findViewById(R.id.tv_show);
        tv_show.setText("0");

        int []ids={R.id.btn_0,R.id.btn_1,R.id.btn_2,R.id.btn_3,R.id.btn_4,
                R.id.btn_4,R.id.btn_5,R.id.btn_6,R.id.btn_7,R.id.btn_8,R.id.btn_9,
                R.id.btn_clear,R.id.btn_divide,R.id.btn_equal,R.id.btn_minus,R.id.btn_multiply,
                R.id.btn_plus};
        for(int i=0;i<ids.length;i++){
            ((Button)findViewById(ids[i])).setOnClickListener(this);
        }

    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.btn_0:
                str_display.append("0");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_1:
                str_display.append("1");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_2:
                str_display.append("2");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_3:
                str_display.append("3");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_4:
                str_display.append("4");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_5:
                str_display.append("5");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_6:
                str_display.append("6");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_7:
                str_display.append("7");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_8:
                str_display.append("8");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_9:
                str_display.append("9");
                tv_show.setText(str_display.toString());
                break;
            case R.id.btn_plus:
                str_oper="+";
                if(!(str_display.toString()=="")){
                    num1+=Double.parseDouble(str_display.toString());
                    str_display=new StringBuffer("");
                    b_add=true;
                }
                if(!(str_result==null)){
                    num1=Double.parseDouble(str_result);
                    str_result=null;
                }
                tv_show.setText(String.valueOf(num1));
                break;
            case R.id.btn_minus:
                str_oper="-";
                if(!b_sub&&!(str_display.toString()=="")){
                    num1=Double.parseDouble(str_display.toString());
                    tv_show.setText(String.valueOf(num1));
                    str_display=new StringBuffer("");
                    b_sub=true;
                }else{
                    if(!(str_display.toString()=="")){
                        num1-=Double.parseDouble(str_display.toString());
                        str_display=new StringBuffer("");
                    }
                    if(!(str_result==null)){
                        num1=Double.parseDouble(str_result);
                        str_result=null;
                    }
                    tv_show.setText(String.valueOf(num1));
                }

                break;
            case R.id.btn_multiply:
                str_oper="×";
                if(!b_mul&&!(str_display.toString()=="")){
                    num1=Double.parseDouble(str_display.toString());
                    tv_show.setText(String.valueOf(num1));
                    str_display=new StringBuffer("");
                    b_mul=true;
                }else{
                    if(!(str_display.toString()=="")){
                        num1*=Double.parseDouble(str_display.toString());
                        str_display=new StringBuffer("");
                    }
                    if(!(str_result==null)){
                        num1=Double.parseDouble(str_result);
                        str_result=null;
                    }
                    tv_show.setText(String.valueOf(num1));
                }

                break;

            case R.id.btn_divide:
            str_oper="÷";
                if(!b_div&&!(str_display.toString()=="")){
                    num1=Double.parseDouble(str_display.toString());
                    tv_show.setText(String.valueOf(num1));
                    str_display=new StringBuffer("");
                    b_div=true;
                }else {
                    if (!(str_display.toString() == "")) {
                        if (Double.parseDouble(str_display.toString())==0) {
                            Toast.makeText(MainActivity.this, "除数不能为零", Toast.LENGTH_LONG).show();
                        } else {
                            num1 /= Double.parseDouble(str_display.toString());
                            str_display = new StringBuffer("");
                        }
                    }
                    if(!(str_result==null)){
                        num1=Double.parseDouble(str_result);
                        str_result=null;
                    }
                    tv_show.setText(String.valueOf(num1));
                }

                break;

            case R.id.btn_clear:
                str_oper="+";
                str_display=new StringBuffer("");
                str_result=null;
                num1=0;
                num2=0;
                b_add=false;
                b_sub=false;
                b_mul=false;
                b_div=false;
                tv_show.setText("0");

            case R.id.btn_equal:
                if(str_oper.equals("+")){
                    num2=Double.parseDouble(str_display.toString());
                    str_result=String.valueOf(num1+num2);
                    tv_show.setText(num1+"+"+num2+"="+str_result);
                    str_display=new StringBuffer("");
                }
                if(str_oper.equals("-")){
                    num2=Double.parseDouble(str_display.toString());
                    str_result=String.valueOf(num1-num2);
                    tv_show.setText(num1+"-"+num2+"="+str_result);
                    str_display=new StringBuffer("");
                }
                if(str_oper.equals("×")){
                    num2=Double.parseDouble(str_display.toString());
                    str_result=String.valueOf(num1*num2);
                    tv_show.setText(num1+"×"+num2+"="+str_result);
                    str_display=new StringBuffer("");
                }
                if(str_oper.equals("÷")){
                    num2=Double.parseDouble(str_display.toString());
                    if(!(num2==0)){
                    str_result=String.valueOf(num1/num2);
                    tv_show.setText(num1+"÷"+num2+"="+str_result);
                    }else{
                        Toast.makeText(MainActivity.this, "除数不能为零", Toast.LENGTH_LONG).show();
                }
                    str_display=new StringBuffer("");
                }
                break;

            default:
                break;
        }

    }
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:676次
    • 积分:161
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档