前面學了許多控件 今天來讓它們都跑起來
今天來做個簡單的計算機
首先先把xml布局弄好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Calculator"
android:gravity="center"
android:textColor="@color/black"
android:textSize="50sp">
</TextView>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="0"
android:textColor="@color/black"
android:textSize="25sp"
android:gravity="right|bottom"
android:lines="3">
</TextView>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="@+id/btn_cancle"
android:text="@string/cancle"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_divide"
android:text="@string/divide"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_multply"
android:text="@string/multiply"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_clear"
android:text="@string/clear"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_seven"
android:text="@string/seven"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_eight"
android:text="@string/eight"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_nine"
android:text="@string/nine"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_plus"
android:text="@string/plus"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_four"
android:text="@string/four"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_five"
android:text="@string/five"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_six"
android:text="@string/six"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_minus"
android:text="@string/minus"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_one"
android:text="@string/one"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_two"
android:text="@string/two"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_three"
android:text="@string/three"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<ImageButton
android:id="@+id/btn_root"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:src="@drawable/text"
android:scaleType="centerInside"
android:background="@drawable/btn_style">
</ImageButton>
<Button
android:id="@+id/btn_reciprocal"
android:text="@string/reciprocal"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_zero"
android:text="@string/zero"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_dot"
android:text="@string/dot"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
<Button
android:id="@+id/btn_equal"
android:text="@string/equal"
android:layout_width="0dp"
android:layout_height="@dimen/btn_height"
android:layout_columnWeight="1"
android:gravity="center"
android:textSize="@dimen/btn_font_size"
android:textColor="@color/black"
android:background="@drawable/btn_style">
</Button>
</GridLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
後端java的程式(只支援兩位的加減乘除:(
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_result;
private String firstNum="";
private String operator="";
private String seconNum="";
private String result="";
private String showtext="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
tv_result = findViewById(R.id.tv_result);
findViewById(R.id.btn_cancle).setOnClickListener(this);
findViewById(R.id.btn_divide).setOnClickListener(this);
findViewById(R.id.btn_multply).setOnClickListener(this);
findViewById(R.id.btn_clear).setOnClickListener(this);
findViewById(R.id.btn_seven).setOnClickListener(this);
findViewById(R.id.btn_eight).setOnClickListener(this);
findViewById(R.id.btn_nine).setOnClickListener(this);
findViewById(R.id.btn_plus).setOnClickListener(this);
findViewById(R.id.btn_four).setOnClickListener(this);
findViewById(R.id.btn_five).setOnClickListener(this);
findViewById(R.id.btn_six).setOnClickListener(this);
findViewById(R.id.btn_minus).setOnClickListener(this);
findViewById(R.id.btn_one).setOnClickListener(this);
findViewById(R.id.btn_two).setOnClickListener(this);
findViewById(R.id.btn_three).setOnClickListener(this);
findViewById(R.id.btn_reciprocal).setOnClickListener(this);
findViewById(R.id.btn_root).setOnClickListener(this);
findViewById(R.id.btn_zero).setOnClickListener(this);
findViewById(R.id.btn_dot).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
}
@Override
public void onClick(View view) {
String inputText;
if(view.getId() == R.id.btn_root){
inputText = "√";
}else {
inputText = ((TextView)view).getText().toString();
}
if(view.getId() == R.id.btn_clear){
clear();
}else if(view.getId() == R.id.btn_plus){
operator = inputText;
refreshText(showtext + operator);
}else if(view.getId() == R.id.btn_minus){
operator = inputText;
refreshText(showtext + operator);
}else if(view.getId() == R.id.btn_multply){
operator = inputText;
refreshText(showtext + operator);
}else if(view.getId() == R.id.btn_divide){
operator = inputText;
refreshText(showtext + operator);
}else if(view.getId() == R.id.btn_equal){
double calculate_result = calculateFour();
refreshOperate(String.valueOf(calculate_result));
refreshText(showtext + "=" +result);
}else if(view.getId() == R.id.btn_reciprocal){
double reciprocal_result = 1 / Double.parseDouble(firstNum);
refreshOperate(String.valueOf(reciprocal_result));
refreshText( "1/"+showtext+"="+result);
}else if(view.getId() == R.id.btn_root){
double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
refreshOperate(String.valueOf(sqrt_result));
refreshText(showtext + "√=" +result);
}else{
if(result.length() > 0 && operator.equals("")){
clear();
}
if(operator.equals("")){
firstNum += inputText;
}else{
seconNum += inputText;
}
if(showtext.equals("0")&&!inputText.equals(".")){
refreshText(inputText);
}else {
refreshText(showtext + inputText);
}
}
}
private double calculateFour() {
if(operator.equals("+")){
return plus(firstNum,seconNum);
}else if(operator.equals("-")){
return minus(firstNum,seconNum);
}else if(operator.equals("x")){
return multiply(firstNum,seconNum);
}else if(operator.equals("÷")){
return divide(firstNum,seconNum);
}else if(operator.equals("")) {
return Double.parseDouble(firstNum);
}else return 0;
}
private void refreshText(String text){
showtext = text;
tv_result.setText(showtext);
}
private void clear(){
refreshText("0");
refreshOperate("");
}
private void refreshOperate(String new_result){
result = new_result;
firstNum = new_result;
seconNum ="";
operator ="";
}
private double plus(String num1,String num2){
return Double.parseDouble(num1) + Double.parseDouble(num2);
}
private double minus(String num1,String num2){
return Double.parseDouble(num1) - Double.parseDouble(num2);
}
private double multiply(String num1,String num2){
return Double.parseDouble(num1) * Double.parseDouble(num2);
}
private double divide(String num1,String num2){
return Double.parseDouble(num1) / Double.parseDouble(num2);
}
}
還有一些邏輯上的東西還要想想 不知道有沒有bug 我沒試出來
今天就這樣嚕 :l