计算器(按钮圆角型 点击变色)

来源:互联网 发布:mac一键切换音频输出 编辑:程序博客网 时间:2024/05/21 17:14

这里写图片描述
在drawable-hdpi下新建selector..xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 点击按钮 -->    <item android:state_pressed="true">        <shape >            <!--  圆角,半径 -->            <corners android:radius="15dp"/>            <!-- 填充,颜色 -->            <solid android:color="#FFA07B"/>        </shape>    </item>   <!--  按钮正常状态,未点击 -->    <item >        <shape >            <!-- 圆角,半径 -->            <corners android:radius="15dp"/>            <!-- 填充,颜色 -->            <solid android:color="#D18263"/>        </shape>    </item></selector>

res/layout/llll.xml

<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:orientation="vertical"     android:background="#FFB5C5"     tools:context="com.itheima.calculitor.MainActivity" >    <TextView        android:id="@+id/tvScreen"        android:layout_width="match_parent"        android:layout_height="150dip"        android:textSize="25dip"        android:gravity="right"        android:background="#FFB5C5"        android:textAppearance="?android:attr/textAppearanceLarge" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#FFB5C5"         android:orientation="horizontal" >        <Button            android:id="@+id/btn7"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="7"               ></Button>        <Button            android:id="@+id/btn8"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="8"               ></Button>        <Button            android:id="@+id/btn9"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="9"               ></Button>        <Button            android:id="@+id/btnDivide"            android:layout_weight="1"            android:background="@drawable/selector"            android:textColor="#4B0082"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="÷"               ></Button>            </LinearLayout>        <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:background="#FFB5C5"        android:orientation="horizontal">        <Button            android:id="@+id/btn4"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="4"               ></Button>        <Button            android:id="@+id/btn5"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="5"               ></Button>        <Button            android:id="@+id/btn6"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="6"               ></Button>        <Button            android:id="@+id/btnX"            android:layout_weight="1"            android:background="@drawable/selector"            android:textColor="#4B0082"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="x"               ></Button>            </LinearLayout>        <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#FFB5C5"        android:orientation="horizontal" >        <Button            android:id="@+id/btn1"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="1"               ></Button>        <Button            android:id="@+id/btn2"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="2"               ></Button>        <Button            android:id="@+id/btn3"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="3"               ></Button>        <Button            android:id="@+id/btnSubtract"            android:layout_weight="1"            android:background="@drawable/selector"            android:textColor="#4B0082"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="-"               ></Button>            </LinearLayout>        <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:background="#FFB5C5"        android:orientation="horizontal">        <Button            android:id="@+id/btnPoint"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="."               ></Button>        <Button            android:id="@+id/btn0"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="0"               ></Button>        <Button            android:id="@+id/btnResult"            android:layout_weight="1"            android:background="@drawable/selector"            android:textColor="#4B0082"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="="           ></Button>        <Button            android:id="@+id/btnAdd"            android:layout_weight="1"            android:background="@drawable/selector"            android:textColor="#4B0082"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="+"               ></Button>            </LinearLayout>        <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:background="#FFB5C5"        android:orientation="horizontal">      <Button            android:id="@+id/btnClear"            android:layout_weight="1"            android:background="@drawable/selector"            android:layout_width="wrap_content"            android:layout_height="wrap_content"              android:text="Clean"               ></Button>            </LinearLayout>    </LinearLayout>

这里写图片描述

Item.java

package com.itheima.calculitor;import android.R.integer;public class Item {    public double value = 0;    public int type = 0;    public Item(double value,int type){        this.value = value;        this.type = type;    }}

MainActivity.java

package com.itheima.calculitor;import java.util.ArrayList;import java.util.List;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;import android.os.Bundle;public class MainActivity extends ActionBarActivity implements OnClickListener {    private TextView tvScreen;    private List<Item> items = new ArrayList<Item>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.llll);        tvScreen = (TextView) findViewById(R.id.tvScreen);        findViewById(R.id.btn0).setOnClickListener(this);        findViewById(R.id.btn1).setOnClickListener(this);        findViewById(R.id.btn2).setOnClickListener(this);        findViewById(R.id.btn3).setOnClickListener(this);        findViewById(R.id.btn4).setOnClickListener(this);        findViewById(R.id.btn5).setOnClickListener(this);        findViewById(R.id.btn6).setOnClickListener(this);        findViewById(R.id.btn7).setOnClickListener(this);        findViewById(R.id.btn8).setOnClickListener(this);        findViewById(R.id.btn9).setOnClickListener(this);        findViewById(R.id.btnAdd).setOnClickListener(this);        findViewById(R.id.btnSubtract).setOnClickListener(this);        findViewById(R.id.btnX).setOnClickListener(this);        findViewById(R.id.btnDivide).setOnClickListener(this);        findViewById(R.id.btnResult).setOnClickListener(this);        findViewById(R.id.btnClear).setOnClickListener(this);        findViewById(R.id.btnPoint).setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {        case R.id.btn0:            tvScreen.append("0");            break;        case R.id.btn1:            tvScreen.append("1");            break;        case R.id.btn2:            tvScreen.append("2");            break;        case R.id.btn3:            tvScreen.append("3");            break;        case R.id.btn4:            tvScreen.append("4");            break;        case R.id.btn5:            tvScreen.append("5");            break;        case R.id.btn6:            tvScreen.append("6");            break;        case R.id.btn7:            tvScreen.append("7");            break;        case R.id.btn8:            tvScreen.append("8");            break;        case R.id.btn9:            tvScreen.append("9");            break;        case R.id.btnPoint:            tvScreen.append(".");            break;          case R.id.btnAdd:            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));               checkandcomputer();            items.add(new Item(0, Types.ADD));            tvScreen.setText("");            break;        case R.id.btnSubtract:            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));               checkandcomputer();            items.add(new Item(0, Types.SUB));            tvScreen.setText("");            break;        case R.id.btnX:            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));               checkandcomputer();            items.add(new Item(0, Types.X));            tvScreen.setText("");            break;        case R.id.btnDivide:            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));               checkandcomputer();            items.add(new Item(0, Types.DIV));            tvScreen.setText("");            break;        case R.id.btnResult:            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));               checkandcomputer();            tvScreen.setText(items.get(0).value+"");            items.clear();            break;        case R.id.btnClear:            tvScreen.setText("");            items.clear();            break;        }    }    public void checkandcomputer(){        if(items.size()>=3){            double a = items.get(0).value;            double b = items.get(2).value;            int operation = items.get(1).type;            items.clear();            switch (operation) {            case Types.ADD:                items.add(new Item(a+b, Types.NUM));                break;            case Types.SUB:                items.add(new Item(a-b, Types.NUM));                break;            case Types.X:                items.add(new Item(a*b, Types.NUM));                break;            case Types.DIV:                items.add(new Item(a/b, Types.NUM));                break;            }        }       }}

Types.java

package com.itheima.calculitor;import android.R.integer;public class Types {    public static final int ADD = 1;    public static final int SUB = 2;    public static final int X = 3;    public static final int DIV = 4;    public static final int NUM = 5;}