自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.example.wyx.exp_2.MainActivity">
<EditText
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:gravity="bottom"
android:hint="0"
android:paddingTop="10dp"
android:textSize="25dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteY="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="@+id/btnQingchu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:text="C"
android:textSize="25dp" />
<Button
android:id="@+id/btnHuishan"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:text="←"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="horizontal">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="7"
android:textSize="25dp" />
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="8"
android:textSize="25dp" />
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="9"
android:textSize="25dp" />
<Button
android:id="@+id/btnChu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="/"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="horizontal">
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="4"
android:textSize="25dp" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="5"
android:textSize="25dp" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="6"
android:textSize="25dp" />
<Button
android:id="@+id/btnCheng"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="*"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="1"
android:textSize="25dp" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="2"
android:textSize="25dp" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="3"
android:textSize="25dp" />
<Button
android:id="@+id/btnJian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="-"
android:textSize="25dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:orientation="horizontal">
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="0"
android:textSize="25dp" />
<Button
android:id="@+id/btnDian"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="."
android:textSize="25dp" />
<Button
android:id="@+id/btnDengyu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="="
android:textSize="25dp" />
<Button
android:id="@+id/btnJia"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:text="+"
android:textSize="25dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity:
package com.example.wyx.exp_2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn0, btn1, btn2, btn3, btn4,
btn5, btn6, btn7, btn8, btn9,
btnJia, btnJian, btnCheng, btnChu,
btnDian, btnDengyu, btnQingchu, btnHuishan;
EditText etInput;
String getText = ""; //存放输入的值
String getText2="";
Double qian=0.0;
Double hou=0.0;
boolean isCounted = false;
//判断是否计算过
public void getButton() {
//获取按钮组件
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);
btnJia = (Button) findViewById(R.id.btnJia);
btnJian = (Button) findViewById(R.id.btnJian);
btnCheng = (Button) findViewById(R.id.btnCheng);
btnChu = (Button) findViewById(R.id.btnChu);
btnDian = (Button) findViewById(R.id.btnDian);
btnDengyu = (Button) findViewById(R.id.btnDengyu);
btnQingchu = (Button) findViewById(R.id.btnQingchu);
btnHuishan = (Button) findViewById(R.id.btnHuishan);
etInput = (EditText) findViewById(R.id.tvResult);
//绑定监听
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);
btnJia.setOnClickListener(this);
btnJian.setOnClickListener(this);
btnCheng.setOnClickListener(this);
btnChu.setOnClickListener(this);
btnDian.setOnClickListener(this);
btnDengyu.setOnClickListener(this);
btnQingchu.setOnClickListener(this);
btnHuishan.setOnClickListener(this);
//输入的字符
getText = etInput.getText().toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getButton();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//数字按钮
case R.id.btn0:
getText += "0";
Log.i("数字0", "" + getText);
break;
case R.id.btn1:
getText += "1";
Log.i("数字1", "" + getText);
break;
case R.id.btn2:
getText += "2";
Log.i("数字2", "" + getText);
break;
case R.id.btn3:
getText += "3";
Log.i("数字3", "" + getText);
break;
case R.id.btn4:
getText += "4";
break;
case R.id.btn5:
getText += "5";
break;
case R.id.btn6:
getText += "6";
break;
case R.id.btn7:
getText += "7";
break;
case R.id.btn8:
getText += "8";
break;
case R.id.btn9:
getText += "9";
break;
case R.id.btnQingchu:
getText = "";
break;
case R.id.btnHuishan:
/**
* 字符串长度大于 0 时才截取字符串
* 如果长度为 1,则直接把字符串设置为 0
*/
if (getText.equals("error")){
getText = "";
} else if (getText.length() > 0){
if (getText.length() == 1) {
getText = "";
} else {
getText = getText.substring(0,getText.length()-1);
}
}
break;
case R.id.btnJia:
if(isCounted==false)
{
qian=qian.parseDouble(getText);
getText+="+";
isCounted=true;
}
else
{
if(getText.contains("+"))
{
getText2=getText.substring(getText.indexOf("+") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("+"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian+hou);
}
else if(getText.contains("-"))
{
getText2=getText.substring(getText.indexOf("-") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("-"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian-hou);
}
else if(getText.contains("*"))
{
getText2=getText.substring(getText.indexOf("*") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("*"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian*hou);
}
else if(getText.contains("/"))
{
getText2=getText.substring(getText.indexOf("/") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("/"));
qian=qian.parseDouble(getText2);
if(hou.equals(0.0))
{
getText="error.";
}
else{
getText=String.valueOf(qian/hou);
}
}
if(getText.contains("error"))
{
getText="";
}
else
getText+="+";
}
break;
case R.id.btnJian:
if(isCounted==false) {
qian = qian.parseDouble(getText);
getText += "-";
isCounted=true;
}
else
{
if(getText.contains("+"))
{
getText2=getText.substring(getText.indexOf("+") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("+"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian+hou);
}
else if(getText.contains("-"))
{
getText2=getText.substring(getText.indexOf("-") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("-"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian-hou);
}
else if(getText.contains("*"))
{
getText2=getText.substring(getText.indexOf("*") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("*"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian*hou);
}
else if(getText.contains("/"))
{
getText2=getText.substring(getText.indexOf("/") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("/"));
qian=qian.parseDouble(getText2);
if(hou.equals(0.0))
{
getText="error.";
}
else{
getText=String.valueOf(qian/hou);
}
}
if(getText.contains("error"))
{
getText="";
}
else
getText+="-";
}
break;
case R.id.btnCheng:
if(isCounted==false) {
qian = qian.parseDouble(getText);
getText += "*";
isCounted=true;
}
else
{
if(getText.contains("+"))
{
getText2=getText.substring(getText.indexOf("+") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("+"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian+hou);
}
else if(getText.contains("-"))
{
getText2=getText.substring(getText.indexOf("-") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("-"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian-hou);
}
else if(getText.contains("*"))
{
getText2=getText.substring(getText.indexOf("*") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("*"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian*hou);
}
else if(getText.contains("/"))
{
getText2=getText.substring(getText.indexOf("/") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("/"));
qian=qian.parseDouble(getText2);
if(hou.equals(0.0))
{
getText="error.";
}
else{
getText=String.valueOf(qian/hou);
}
}
if(getText.contains("error"))
{
getText="";
}
else
getText+="*";
}
break;
case R.id.btnChu:
if(isCounted==false) {
qian = qian.parseDouble(getText);
getText += "/";
isCounted=true;
}
else
{
if(getText.contains("+"))
{
getText2=getText.substring(getText.indexOf("+") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("+"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian+hou);
}
else if(getText.contains("-"))
{
getText2=getText.substring(getText.indexOf("-") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("-"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian-hou);
}
else if(getText.contains("*"))
{
getText2=getText.substring(getText.indexOf("*") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("*"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian*hou);
}
else if(getText.contains("/"))
{
getText2=getText.substring(getText.indexOf("/") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("/"));
qian=qian.parseDouble(getText2);
if(hou.equals(0.0))
{
getText="error.";
}
else{
getText=String.valueOf(qian/hou);
}
}
if(getText.contains("error"))
{
getText="";
}
else
getText+="/";
}
break;
case R.id.btnDengyu:
if(getText.contains("+"))
{
getText2=getText.substring(getText.indexOf("+") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("+"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian+hou);
isCounted=false;
}
else if(getText.contains("-"))
{
getText2=getText.substring(getText.indexOf("-") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("-"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian-hou);
isCounted=false;
}
else if(getText.contains("*"))
{
getText2=getText.substring(getText.indexOf("*") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("*"));
qian=qian.parseDouble(getText2);
getText=String.valueOf(qian*hou);
isCounted=false;
}
else if(getText.contains("/"))
{
getText2=getText.substring(getText.indexOf("/") + 1);
hou=hou.parseDouble(getText2);
getText2=getText.substring(0,getText.indexOf("/"));
qian=qian.parseDouble(getText2);
if(hou.equals(0.0))
{
getText="error.";
}
else{
getText=String.valueOf(qian/hou);
}
isCounted=false;
}
case R.id.btnDian:
if (getText.contains("+") || getText.contains("-") ||
getText.contains("*") || getText.contains("/"))
{
getText+=".";
}
else if(!getText.contains("."))
getText+=".";
break;
}
etInput.setText(getText);
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wyx.exp_2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>