參考博文:安卓簡易電腦
自己重新編寫了布局,MainActivity也做了一些小的更改,代碼更舒服了一些,邏輯沒有什麼改動。
效果圖和功能說明
功能:
1.隻能進行簡單的兩操作數的加減乘除
2.清空
3.回退一個輸入的字元
效果:
代碼
1.布局檔案activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/textView1"
android:layout_width="354dp"
android:layout_height="64dp"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="57dp"
android:layout_marginBottom="158dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000"
android:textSize="28dp"
app:layout_constraintBottom_toTopOf="@+id/buttonPanel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText1"
android:layout_width="411dp"
android:layout_height="0dp"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:layout_marginBottom="17dp"
android:ems="10"
android:singleLine="true"
android:textColor="#000"
android:textSize="35dp"
app:layout_constraintBottom_toTopOf="@+id/buttonPanel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TableLayout
android:id="@+id/buttonPanel"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="9dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonH"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btH"
android:textSize="23dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="274dp" />
<Button
android:id="@+id/buttonClear"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btClear"
android:textSize="23dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="274dp" />
<Button
android:id="@+id/buttonBack"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btBack"
android:textSize="23dp"
tools:layout_editor_absoluteX="105dp"
tools:layout_editor_absoluteY="274dp" />
<Button
android:id="@+id/buttonOp3"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btOp3"
android:textSize="23dp"
tools:layout_editor_absoluteX="305dp"
tools:layout_editor_absoluteY="402dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonNum1"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum1"
android:textSize="23dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="338dp" />
<Button
android:id="@+id/buttonNum2"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum2"
android:textSize="23dp"
tools:layout_editor_absoluteX="105dp"
tools:layout_editor_absoluteY="338dp" />
<Button
android:id="@+id/buttonNum3"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum3"
android:textSize="23dp"
tools:layout_editor_absoluteX="195dp"
tools:layout_editor_absoluteY="338dp" />
<Button
android:id="@+id/buttonOp1"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="+"
tools:layout_editor_absoluteX="305dp"
tools:layout_editor_absoluteY="338dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonNum4"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum4"
android:textSize="23dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="402dp" />
<Button
android:id="@+id/buttonNum5"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum5"
android:textSize="23dp"
tools:layout_editor_absoluteX="106dp"
tools:layout_editor_absoluteY="402dp" />
<Button
android:id="@+id/buttonNum6"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum6"
android:textSize="23dp"
tools:layout_editor_absoluteX="196dp"
tools:layout_editor_absoluteY="402dp" />
<Button
android:id="@+id/buttonOp2"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btOp2"
android:textSize="23dp"
tools:layout_editor_absoluteX="305dp"
tools:layout_editor_absoluteY="337dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonNum7"
android:layout_width="105dp"
android:layout_height="64dp"
android:text="@string/btNum7"
android:textSize="23dp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="467dp" />
<Button
android:id="@+id/buttonNum8"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum8"
android:textSize="23dp"
tools:layout_editor_absoluteX="105dp"
tools:layout_editor_absoluteY="468dp" />
<Button
android:id="@+id/buttonNum9"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum9"
android:textSize="23dp"
tools:layout_editor_absoluteX="195dp"
tools:layout_editor_absoluteY="467dp" />
<Button
android:id="@+id/buttonOp4"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btOp4"
android:textSize="23dp"
tools:layout_editor_absoluteX="305dp"
tools:layout_editor_absoluteY="468dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonPoint"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btPoint"
android:textSize="23dp"
tools:layout_editor_absoluteX="195dp"
tools:layout_editor_absoluteY="534dp" />
<Button
android:id="@+id/buttonNum0"
android:layout_width="105dp"
android:layout_height="65dp"
android:text="@string/btNum0"
android:textSize="23dp"
tools:layout_editor_absoluteX="10dp"
tools:layout_editor_absoluteY="534dp" />
<Button
android:id="@+id/buttonEqual"
android:layout_width="105dp"
android:layout_height="65dp"
android:layout_weight="2"
android:text="@string/btEq"
android:textSize="23dp"
tools:layout_editor_absoluteX="306dp"
tools:layout_editor_absoluteY="534dp" />
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>
2.strings.xml
<resources>
<string name="app_name">MyCalculator</string>
<string name="btNum1">1</string>
<string name="btNum2">2</string>
<string name="btNum3">3</string>
<string name="btNum4">4</string>
<string name="btNum5">5</string>
<string name="btNum6">6</string>
<string name="btNum7">7</string>
<string name="btNum8">8</string>
<string name="btNum9">9</string>
<string name="btNum0">0</string>
<string name="btPoint">.</string>
<string name="btOp1">+</string>
<string name="btOp2">-</string>
<string name="btOp3">*</string>
<string name="btOp4">÷</string>
<string name="btBack">back</string>
<string name="btClear">C</string>
<string name="btH">%</string>
<string name="btEq">=</string>
<string name="btTextSize">23dp</string>
<string name="btWidth">105dp</string>
<string name="btHeight">65dp</string>
</resources>
3.MainActivity.java
我們來理一下如何擷取editText以及TextView中的内容:
1.聲明對應類型的私有變量
2.将變量通過
findViewById(R.id.xxx)
進行執行個體化
3.擷取控件上的字元串(以editText為例):
editText.getText().toString();
4.設定控件上的文字(以editText為例):
editText.setText()
package com.example.yogi.mycalculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private Button btn[] = new Button[18];
private EditText editText;//顯示輸入的數字
private TextView textView;
private String opt = "+";//操作符
private double n1 = 0.0, n2 = 0.0;//兩個操作數
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//執行個體化 擷取按鈕的id
btn[0] = (Button) findViewById(R.id.buttonNum0);
btn[1] = (Button) findViewById(R.id.buttonNum1);
btn[2] = (Button) findViewById(R.id.buttonNum2);
btn[3] = (Button) findViewById(R.id.buttonNum3);
btn[4] = (Button) findViewById(R.id.buttonNum4);
btn[5] = (Button) findViewById(R.id.buttonNum5);
btn[6] = (Button) findViewById(R.id.buttonNum6);
btn[7] = (Button) findViewById(R.id.buttonNum7);
btn[8] = (Button) findViewById(R.id.buttonNum8);
btn[9] = (Button) findViewById(R.id.buttonNum9);
btn[10] = (Button) findViewById(R.id.buttonOp1);
btn[11] = (Button) findViewById(R.id.buttonOp2);
btn[12] = (Button) findViewById(R.id.buttonOp3);
btn[13] = (Button) findViewById(R.id.buttonOp4);
btn[14] = (Button) findViewById(R.id.buttonEqual);
btn[15] = (Button) findViewById(R.id.buttonPoint);
btn[16] = (Button) findViewById(R.id.buttonClear);
btn[17] = (Button) findViewById(R.id.buttonBack);
editText = (EditText)findViewById(R.id.editText1);
textView = (TextView)findViewById(R.id.textView1);
//為按鈕添加監聽器
for(int i=0;i<18;i++){
btn[i].setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s = editText.getText().toString();//擷取字元串
try{
switch(v.getId())
{
case R.id.buttonNum1: case R.id.buttonNum2: case R.id.buttonNum3:
case R.id.buttonNum4: case R.id.buttonNum5: case R.id.buttonNum6:
case R.id.buttonNum7: case R.id.buttonNum8: case R.id.buttonNum9:
case R.id.buttonNum0:
{
String str = editText.getText().toString();
editText.setText(str + ((Button) v).getText());
break;
}
case R.id.buttonEqual://操作符=
{
if(opt == "+")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 + n2) + "");
}
else if(opt == "-")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 - n2) + "");
}
else if(opt == "*")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 * n2) + "");
}
else if(opt == "/")
{
String str = editText.getText().toString();
n2 = Double.parseDouble(str);
if(n2 == 0)
{
editText.setText("");
textView.setText("除數不能為0");
break;
}
else
{
textView.setText(n1 + opt + n2 + "=");
editText.setText((n1 / n2) + "");
}
}
break;
}
case R.id.buttonPoint://.
{
String str = editText.getText().toString();
if(str.indexOf(".") != -1) //判斷字元串中是否已經包含了小數點,如果有就什麼也不做
{
}
else //如果沒有小數點
{
if(str.equals("0"))//如果開始為0,
editText.setText(("0" + ".").toString());
else if(str.equals(""))//如果初時顯示為空,就什麼也不做
{
}
else
editText.setText(str + ".");
}
break;
}
case R.id.buttonOp4://操作符 /
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "/";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.buttonOp3://操作符*
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "*";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.buttonOp2://操作符-
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "-";
editText.setText("");
textView.setText(n1 + opt);
break;
}
case R.id.buttonOp1://+
{
String str = editText.getText().toString();
n1 = Double.parseDouble(str);
opt = "+";
textView.setText(n1 + opt);
editText.setText("");
break;
}
case R.id.buttonClear://C
{
String str =editText.getText().toString();
if(str.length() > 0)
editText.setText("");
textView.setText("");
break;
}
case R.id.buttonBack://back
{
String str =editText.getText().toString();
if(str.length() > 0)
editText.setText(str.substring(0, str.length() - 1));
break;
}
}
}catch(Exception e){}
}
}