才学android,根据视频写了写了一个功能简单的计算器。
1.MainActivity.java
package com.example.xuhai.calculate;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn0;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btn_add;
private Button btn_miuns;
private Button btn_point;
private Button btn_clear;
private Button btn_equal;
private Button btn_del;
private Button btn_multiply;
private Button btn_divide;
private EditText et;
private String str;
private boolean clear_flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_equal = (Button) findViewById(R.id.btn_equal);
btn_point = (Button) findViewById(R.id.btn_point);
btn_add = (Button) findViewById(R.id.btn_add);
btn_miuns = (Button) findViewById(R.id.btn_minus);
btn_multiply = (Button) findViewById(R.id.btn_multiply);
btn_divide = (Button) findViewById(R.id.btn_divide);
et = (EditText) findViewById(R.id.et_input);
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_miuns.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
str = et.getText().toString();
switch (v.getId()) {
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn5:
case R.id.btn4:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.btn_point:
if(clear_flag)
{
clear_flag=false;
str="";
et.setText("");
}
et.setText(str + ((Button) v).getText());
break;
case R.id.btn_add:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
if(clear_flag)
{
clear_flag=false;
str="";
et.setText("");
}
et.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.btn_clear:
clear_flag=false;
et.setText("");
break;
case R.id.btn_del:
if (str != null && !str.equals("")) {
et.setText(str.substring(0, str.length() - 1));
}
clear_flag=false;
break;
case R.id.btn_equal:
String str1 = et.getText().toString();
if(str1 ==null||str1.equals("")||clear_flag==true)
et.setText("");
else
getResult();
break;
}
}
private void getResult() {
clear_flag=true;
String exp = et.getText().toString();
double resule = 0;
String s1 = exp.substring(0, exp.indexOf(" "));
String op = exp.substring(exp.indexOf(" ") + 1, exp.indexOf(" ") + 2);
String s2 = exp.substring(exp.indexOf(" ") + 3);
if (!s1.equals("") && !s2.equals("")) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
resule = d1 + d2;
} else if (op.equals("-")) {
resule = d1 - d2;
} else if (op.equals("*")) {
resule = d1 * d2;
} else if (op.equals("/")) {
if (d2 == 0)
resule = 0;
else
resule = d1 / d2;
}
if (!s1.contains(".") && !s2.contains(".")&&!op.equals("/")) {
int r = (int) resule;
et.setText(r + "");
} else
et.setText(resule + "");
} else if (!s1.equals("") && s2.equals("")) {
et.setText(exp);
} else if (s1.equals("") && !s2.equals("")) {
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
resule = 0 + d2;
} else if (op.equals("-")) {
resule = 0 - d2;
} else if (op.equals("*")) {
resule = 0;
} else if (op.equals("/")) {
if (d2 == 0)
resule = 0;
}
if ( !s2.contains(".")) {
int r = (int) resule;
et.setText(r + "");
} else
et.setText(resule + "");
}else{
et.setText("");
}
}
}
2.content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.xuhai.calculate.MainActivity"
android:background="@android:color/background_dark"
tools:showIn="@layout/activity_main">
<EditText
android:layout_width="fill_parent"
android:layout_height="60dip"
android:id="@+id/et_input"
android:editable="false"
android:gravity="right|bottom"
android:background="@drawable/white_bg"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginTop="70dp"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/linearLayout">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="C"
android:textSize="20sp"
android:id="@+id/btn_clear"
android:background="@drawable/white_selector"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingRight="10dp"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="DEL"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_del"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="right|bottom"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="/"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_divide"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="*"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_multiply"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginTop="140dp"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/linearLayout1">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="7"
android:textSize="20sp"
android:id="@+id/btn7"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="right|bottom"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="8"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn8"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="right|bottom"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="9"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn9"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="-"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_minus"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_marginTop="210dp"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/linearLayout2">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="4"
android:textSize="20sp"
android:id="@+id/btn4"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="right|bottom"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="5"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn5"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="right|bottom"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="6"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn6"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_add"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="140dp"
android:layout_marginTop="280dp"
android:orientation="horizontal"
android:gravity="center"
android:id="@+id/linearLayout3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:textSize="20sp"
android:id="@+id/btn1"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="2"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn2"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="3"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn3"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="140dp"
android:layout_height="60dp"
android:text="0"
android:textSize="20sp"
android:id="@+id/btn0"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="."
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_point"
android:background="@drawable/white_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="60dp"
android:layout_height="140dp"
android:text="="
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:id="@+id/btn_equal"
android:background="@drawable/orange_selector"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:gravity="bottom|right"/>
</LinearLayout>
</RelativeLayout>
3.颜色配图xml
white_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:drawable="@drawable/gray_bg" android:state_pressed="true"/>
<item android:drawable="@drawable/white_bg" />
</selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <item android:drawable="@drawable/orange_bg" android:state_pressed="true"/> <item android:drawable="@drawable/ashend_bg" /> </selector>ashend_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <corners android:radius="5dp"/> <solid android:color="#ff7f50"/> </shape>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"/> <!--<gradient android:startColor="#ffffff" android:endColor="#ffa500"/> <stroke android:width="1dp" android:color="#000000"/> />--> </shape>
这是全部的源代码:知识点总结
1.str = et.getText().toString();//得到EditText的值,String类型的
2.et.setText(str + ((Button) v).getText());//设置EditText的值,将v设置成Button
3.substring(0, str.length() - 1));//截取从0开始到字符的倒数第二位
4.if(str1 ==null||str1.equals("")||clear_flag==true)//在这个里面,null用“==”,而“”用的是equal注意这一点;
5.indexOf()函数的用法:字符串的IndexOf()方法搜索在该字符串上是否出现了作为参数传递的字符串,如果找到字符串,则返回字符的起始位置 (0表示第一个字符,1表示第二个字符依此类推)如果说没有找到则返回 -1
返回 String 对象内第一次出现子字符串的字符位置。例如:String s1 = exp.substring(0, exp.indexOf(" "));
6.double d1 = Double.parseDouble(s1);//将s1是String类型强制转换为double类型。
7.s1.contains(".")//检测字符串内是否出现“.”这个字符串。