自己重新编写了布局,MainActivity也做了一些小的更改,代码更舒服了一些,逻辑没有什么改动。
功能:
1.只能进行简单的两操作数的加减乘除
2.清空
3.回退一个输入的字符
<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>
<resources>
<string name="app_name">MyCalculatorstring>
<string name="btNum1">1string>
<string name="btNum2">2string>
<string name="btNum3">3string>
<string name="btNum4">4string>
<string name="btNum5">5string>
<string name="btNum6">6string>
<string name="btNum7">7string>
<string name="btNum8">8string>
<string name="btNum9">9string>
<string name="btNum0">0string>
<string name="btPoint">.string>
<string name="btOp1">+string>
<string name="btOp2">-string>
<string name="btOp3">*string>
<string name="btOp4">÷string>
<string name="btBack">backstring>
<string name="btClear">Cstring>
<string name="btH">%string>
<string name="btEq">=string>
<string name="btTextSize">23dpstring>
<string name="btWidth">105dpstring>
<string name="btHeight">65dpstring>
resources>
我们来理一下如何获取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){}
}
}