首页 > 其他分享 >android打造的最简单计算器界面

android打造的最简单计算器界面

时间:2023-04-07 10:02:37浏览次数:49  
标签:界面 text Button editText 计算器 clicked android btn setText


先看图:



这里主要是锻炼一下TableLayout布局,注意其中的android:stretchColumns="0,1,2,3"属性,该属性可以控制每列的宽的权重,类似weight,由于这里4列都是“平等的”,所以是“0,1,2,3”,全部布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <EditText 
    android:id="@+id/result" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:text=""
	android:singleLine="true"
	android:numeric="decimal"
	android:focusable="false"
	android:digits="1234567890."
	android:cursorVisible="false"
	/>
	<TableLayout 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:stretchColumns="0,1,2,3">
		<TableRow>
			<Button android:id="@+id/btn_1" android:text="1"/>
			<Button android:id="@+id/btn_2" android:text="2" />
			<Button android:id="@+id/btn_3" android:text="3" />
			<Button android:id="@+id/btn_add" android:text="+" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_4" android:text="4" />
			<Button android:id="@+id/btn_5" android:text="5" />
			<Button android:id="@+id/btn_6" android:text="6" />
			<Button android:id="@+id/btn_sub" android:text="-" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_7" android:text="7" />
			<Button android:id="@+id/btn_8" android:text="8" />
			<Button android:id="@+id/btn_9" android:text="9" />
			<Button android:id="@+id/btn_mul" android:text="*" />
		</TableRow>
		<TableRow>
			<Button android:id="@+id/btn_dot" android:text="." />
			<Button android:id="@+id/btn_0" android:text="0" />
			<Button android:id="@+id/btn_eq" android:text="=" />
			<Button android:id="@+id/btn_div" android:text="/" />
		</TableRow>
	</TableLayout>
	<Button 
	android:id="@+id/btn_clear" 
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content"
	android:text="clear"
	/>
</LinearLayout>



java源码如下,这代码我承认写的不好,主要是数字键button没有使用数组的形式,这样很拖沓,将就着看吧!


package com.wt.app;

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

public class App extends Activity {
	
	EditText editText;
	StringBuffer text;
	String number_1="",number_2="";
	boolean clicked=false;
	byte style=0;
	Button btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_0;
	Button btn_add,btn_sub,btn_mul,btn_div,btn_eq,btn_dot,btn_clear;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        text=new StringBuffer();
        editText=(EditText)findViewById(R.id.result);
        //editText.setFocusable(false);
        btn_1=(Button) findViewById(R.id.btn_1);
        btn_1.setOnClickListener(new Button.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
				text.append("1");
				editText.setText(text.toString());
				
			}
        	
        });
        btn_2=(Button) findViewById(R.id.btn_2);
        btn_2.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("2");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_3=(Button) findViewById(R.id.btn_3);
        btn_3.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("3");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_4=(Button) findViewById(R.id.btn_4);
        btn_4.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("4");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_5=(Button) findViewById(R.id.btn_5);
        btn_5.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("5");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_6=(Button) findViewById(R.id.btn_6);
        btn_6.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("6");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_7=(Button) findViewById(R.id.btn_7);
        btn_7.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("7");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_8=(Button) findViewById(R.id.btn_8);
        btn_8.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("8");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_9=(Button) findViewById(R.id.btn_9);
        btn_9.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("9");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_0=(Button) findViewById(R.id.btn_0);
        btn_0.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		text.append("0");
        		editText.setText(text.toString());
        	}
        	
        });
        btn_dot=(Button) findViewById(R.id.btn_dot);
        btn_dot.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		if(clicked){
					editText.setText("");
					clicked=false;
					text.setLength(0);
				}
        		if(editText.getText().toString().indexOf(".")==-1){//已经有.了
        			if(text.length()==0){
        				text.append("0.");
        			}else{
        				text.append(".");
        			}
        			editText.setText(text.toString());
        		}
        	}
        	
        });
        
        btn_add=(Button) findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=1;
        		clicked=true;
        	}
        	
        });
        btn_sub=(Button) findViewById(R.id.btn_sub);
        btn_sub.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=2;
        		clicked=true;
        	}
        	
        });
        btn_mul=(Button) findViewById(R.id.btn_mul);
        btn_mul.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=3;
        		clicked=true;
        	}
        	
        });
        btn_div=(Button) findViewById(R.id.btn_div);
        btn_div.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_1=editText.getText().toString();
        		style=4;
        		clicked=true;
        	}
        	
        });
        btn_eq=(Button) findViewById(R.id.btn_eq);
        btn_eq.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		number_2=editText.getText().toString();
        		if(number_1.equals("")&&number_2.equals("")){
        			return;
        		}
        		Double d1=Double.valueOf(number_1);
        		Double d2=Double.valueOf(number_2);
        		Double r=0.0;;
        		switch (style) {
				case 1:
					r=d1+d2;
					break;
				case 2:
					r=d1-d2;
					break;
				case 3:
					r=d1*d2;
					break;
				case 4:
					r=d1/d2;
					break;
				}
        		editText.setText(r.toString());
        	}
        	
        });
        btn_clear=(Button) findViewById(R.id.btn_clear);
        btn_clear.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		editText.setText("");
        		number_1="";
        		number_2="";
        		style=0;
        		clicked=false;
        		text.setLength(0);
        	}
        	
        });
    }
    
}




Android——Tabel布局,计算器的实现


  • android打造的最简单计算器界面_xml

  • 大小: 21.9 KB
  • 查看图片附件

标签:界面,text,Button,editText,计算器,clicked,android,btn,setText
From: https://blog.51cto.com/u_5454003/6174604

相关文章

  • android之简单数据存储Preference
    这里的持久化其实就是本地配置文件的读写,实现方法是通过Activity.getPreferences(int)获取SharedPreferences对象,然后操作配置文件的读写,值得注意的是以下几点:1)Activity.getPreferences(intmode)等价于Content.getSharedPreferences(Stringfilename,intmod......
  • Android存储用户登录信息最好的方式之一-Shared Preferences
    对于Android应用程序,存储用户登录信息的最佳方式是使用SharedPreferences。SharedPreferences是Android提供的一个轻量级存储机制,可以存储简单的键值对数据。它非常适合存储用户设置、用户偏好和其他应用程序数据,包括登录信息。SharedPreferences具有以下优点:轻量级:Shared......
  • 如何获取 Android 设备的CPU核数、时钟频率以及内存大小
    DeviceYearClass的主要功能是根据CPU核数、时钟频率以及内存大小对设备进行分级。下表是Facebook公司提供的分级标准,其中Year栏表示分级结果。以下类就是从Facebook的开源项目DeviceYearClass中拿出来的packagecom.yirui.youbao.util;importandroid.annotati......
  • 今日总结 Android与硬件设备通信(一)
    今天看了看Android与硬件通信的原理1、建立连接的原理在实现软件与硬件交互的时候,首先需要了解该硬件的构造,运行流程等相关操作,熟读相关的文档,知道硬件开发商那边给出了哪些接口,分别对应我们项目中的那些模块等等就拿我这个项目的硬件来说,供应商给出的连接流程如下图:  我......
  • Android中asset文件夹和raw文件夹区别
    res/raw和assets的相同点:1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。res/raw和assets的不同点:1.res/raw中的文件会被映射到R.java文件中,访问的时候直接使用资源ID即R.id.filename;assets文件夹下的文件不会被映射到R.java......
  • Android service的完全详解
    Service全部内容基本会在本篇涉及到,我们将围绕以下主要知识点进行分析:Service简单概述Service在清单文件中的声明Service启动服务实现方式及其详解Service绑定服务的三种实现方式关于启动服务与绑定服务间的转换问题前台服务以及通知发送服务Service与线程Thread的区别管理......
  • 可左右两侧挤压傍边布局的Android抽屉
    我参考了这篇文章,我将它改了一下:可动态布局的Android抽屉之基础工程中需要这样的效果,左边和右边的Panel可以打开关闭:我把左边和右边的Panel封装成2个类了。这里要特别注意,抽屉是需要“handler”的,我这里可以把任何View都看成“handler”,使用setBindView(Vie......
  • Android自定义捕获Application全局异常
    参考:http://bigcat.easymorse.com/?p=1152packageqianlong.qlmobile.ui;importjava.io.File;importjava.io.FileOutputStream;importjava.io.FilenameFilter;importjava.io.PrintWriter;importjava.io.StringWriter;importjava.io.Writer......
  • Android播放GIF动画
    Android支持GIF动画,但是如果利用ImageView标签直接写在布局文件中:<ImageViewandroid:id="@+id/gifpicture"android:layout_width="fill_parent"android:layout_height="wrap_content"android:src="@drawable/animation"......
  • 若依界面修改
    阿里图标一( ̄︶ ̄*))图片白嫖一((* ̄3 ̄)╭*********专栏略长====爆肝万字====细节狂魔====请准备好一键三连*********运行成功后:idea后台正常先挂着我习惯用VScode操作当然如果有两台机子一个挂后台一个改前端就更好了只需修改vue.config.js配置文件即可eg:按 Win+R打开......