Android学习小案例——计算器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40205116/article/details/88550843

上一篇讲了android的六大基本布局:https://blog.csdn.net/qq_40205116/article/details/88418781

这一篇来用布局做一个android小项目(计算器),效果如下(本来想插入视频的,但发现好像插入不了视频,只能插入图片了,这里%表示取余):

分析:主要路基思维在于加减乘除四则运算的优先级,把这个解决了也就没什么难的了,可以把等式先化成字符数组,在转换成字符集合(数和运算符),之后进行计算。计算时第一个数字符串的下标为0,只用判断第二字符串运算符是否为乘或除或取余。

1.如果是这直接进行计算,之后把结果放在下标为0的集合中,删除后面两个元素

2.如果不是,则判断下标为3的运算符是否为乘或除或取余。是则运算,不是则可以运算前面的一组。

以此来推,如果还不理解那就看代码吧(表达能力不是很好O(∩_∩)O哈哈~)!

代码部分:

activity_main.xml中:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
	android:stretchColumns="*" >

    <!-- 显示的文本 -->
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@android:color/holo_blue_bright"
        android:textSize="20dp"
        android:gravity="center|right"
        android:text="" />
    
    <!-- 
    	android:onClick="fun"设置点击事件
    	android:tag="%"点击获取的文本值
     -->
    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:id="@+id/btn01"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="C" />
        <Button
            android:id="@+id/btn02"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="←" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="%"
            android:text="%" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="÷"
            android:text="÷" />
    </TableRow>
	<TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="7"
            android:text="7" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="8"
            android:text="8" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="9"
            android:text="9" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="x"
            android:text="x" />
    </TableRow>
    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="4"
            android:text="4" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="5"
            android:text="5" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="6"
            android:text="6" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="-"
            android:text="-" />
    </TableRow>
    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="1"
            android:text="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="2"
            android:text="2" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="3"
            android:text="3" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="+"
            android:text="+" />
    </TableRow>
    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="."
            android:text="." />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="fun"
            android:tag="0"
            android:text="0" />
        <Button
            android:id="@+id/btn19"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="=" />
    </TableRow>
</TableLayout>

MainActivity.java类:

package com.example.tablelayoutjsq;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView text;
	private Button btn01;
	private Button btn02;
	private Button btn19;
	String str;		//文本值
	String str1 = "";	//加减乘除等式
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			String s = "";
			switch (v.getId()) {
			case R.id.btn01:	//清空
				text.setText("");
				str1 = "";
				break;
			case R.id.btn02:	//回退
				if(text.getText().toString().length() > 0){
					s = text.getText().toString().substring(0, text.getText().toString().length()-1);					
					text.setText(s);
				}
				break;
			case R.id.btn19:	//点击=号算结果
				str = text.getText().toString();
				if(str.equals("")){
					return ;
				}
				char[] array = str.toCharArray();
				List<String> list = new ArrayList<String>();
				for(int i = str1.length(); i < array.length; i++){
					if(array[i] == '%' || array[i] == '÷' || array[i] == 'x' || array[i] == '-' || array[i] == '+'){
						if(!s.equals("")){
							list.add(s);
							s = "";
						}
						String a = array[i]+"";
						list.add(a);
					}else{
						s += array[i];
						if(i == array.length-1){
							list.add(s);
							s = "";
						}
					}
				}
				while(true){
					String b = list.get(list.size()-1);
					if(b.equals("%") || b.equals("÷") || b.equals("x") || b.equals("+") || b.equals("-"))
						list.remove(list.size()-1);
					if(list.size() == 1){
						str1 = text.getText().toString()+"\n";
						text.setText(str1+list.get(0));
						break;
					}
					if(list.size() == 3){
						fun(0, list);
					}else{
						if("%".equals(list.get(3)) || "÷".equals(list.get(3)) || "x".equals(list.get(3))){
							fun(2, list);
						}else{
							fun(0, list);
						}
					}
				}
			default:
				break;
			}
		}
		//加减乘除运算方法
		private void fun(int i, List<String> list) {
			double x = Double.parseDouble(list.get(i));
			String y = list.get(i+1);
			double z = Double.parseDouble(list.get(i+2));
			String sum;
			if(y.equals("%")){
				sum = x % z+"";
			}else if(y.equals("÷")){
				sum = x / z+"";
			}else if(y.equals("+")){
				sum = x + z+"";
			}else if(y.equals("-")){
				sum = x - z+"";
			}else{
				sum = x * z+"";
			}
			list.set(i, sum);
			list.remove(i+2);
			list.remove(i+1);
		}
	};
	//点击按钮,把按钮值显示在文本上
	public void fun(View v){
		str = text.getText().toString();
		if(str.length() > 0){
			char c = str.charAt(str.length()-1);
			String ss = (String) v.getTag();
			if((c == '%' || c == '÷' || c == 'x' || c == '+' || c == '-') && (ss.equals("%") || ss.equals("÷") || ss.equals("x") || ss.equals("+") || ss.equals("-"))){
				str = str.substring(0, str.length()-1)+ss;
				text.setText(str);
			}else{
				text.append((String)v.getTag());
			}
		}else{
			text.append((String)v.getTag());	//append在字符串后拼接一个			
		}
	}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView) findViewById(R.id.text);
        btn01 = (Button) findViewById(R.id.btn01);
        btn02 = (Button) findViewById(R.id.btn02);
        btn19 = (Button) findViewById(R.id.btn19);
        btn01.setOnClickListener(listener);
        btn02.setOnClickListener(listener);
        btn19.setOnClickListener(listener);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40205116/article/details/88550843