技术标签: android开发 java android小程序
注:现在对于eclipse开发的android项目可以直接导入到androidstudio中。
android开发初学的第一个程序:
1.先构建android项目
2.构建出简单的计算器界面
3.对按键布局进行动作监听设置,并作出相应的反馈操作
4.打开虚拟机调试程序
操作1得到如下:
操作2之后:
对于计算器的背景图片是我自己添加的,需要把该图片复制到res/drawable开头的文件夹下,每个都要。
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bgi"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入:" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="@+id/button17"
android:layout_below="@+id/textView1"
android:background="#666"
android:ems="10"
android:gravity="center_vertical|right"
android:singleLine="true"
android:textColor="#000"
android:textSize="28dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="@+id/editText1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/editText1"
android:layout_marginBottom="10dp"
android:background="#666"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="15dp"
android:gravity="center_vertical|right" />
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_CE"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_CE" />
<Button
android:id="@+id/button_DELETE"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_DELETE" />
<Button
android:id="@+id/button_XX"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_XX" />
<Button
android:id="@+id/button_ADD"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_ADD" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_1"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_1" />
<Button
android:id="@+id/button_2"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_2" />
<Button
android:id="@+id/button_3"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_3" />
<Button
android:id="@+id/button_SUB"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_SUB" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_4"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_4" />
<Button
android:id="@+id/button_5"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_5" />
<Button
android:id="@+id/button_6"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_6" />
<Button
android:id="@+id/button_MUL"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_X" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_7"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_7" />
<Button
android:id="@+id/button_8"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_8" />
<Button
android:id="@+id/button_9"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_9" />
<Button
android:id="@+id/button_DEV"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_DEV" />
</TableRow>
</TableLayout>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/button_dot"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_dot" />
<Button
android:id="@+id/button_0"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/button_0" />
<Button
android:id="@+id/button_EQU"
android:layout_width="160dp"
android:layout_height="60dp"
android:text="@string/button_EQU" />
</TableRow>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Caculator</string>
<string name="hello_world">Hello world!</string>
<string name="button_CE">CE</string>
<string name="button">DELETE</string>
<string name="button_DELETE">DEL</string>
<string name="button_XX">x²</string>
<string name="button_ADD">+</string>
<string name="button_1">1</string>
<string name="button_2">2</string>
<string name="button_3">3</string>
<string name="button_SUB">-</string>
<string name="button_4">4</string>
<string name="button_5">5</string>
<string name="button_6">6</string>
<string name="button_X">×</string>
<string name="button_7">7</string>
<string name="button_8">8</string>
<string name="button_9">9</string>
<string name="button_DEV">÷</string>
<string name="button_0">0</string>
<string name="button_dot">.</string>
<string name="button_EQU">=</string>
</resources>
接下来就是对已经布局好的案件操作进行监听并对响应的操作进行反馈:
src/Mainactivity.java文件:
package com.example.caculator;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import bsh.EvalError;
import bsh.Interpreter;
public class MainActivity extends Activity {
//声明变量
private Button button_1;
private Button button_2;
private Button button_3;
private Button button_4;
private Button button_5;
private Button button_6;
private Button button_7;
private Button button_8;
private Button button_9;
private Button button_0;
private Button button_dot;
private Button button_ADD;
private Button button_SUB;
private Button button_MUL;
private Button button_DEV;
private Button button_EQU;
private Button button_DEL;
private Button button_XX;
private Button button_CE;
private EditText edittext;
static TextView textview;
static String password="";
static int index;
boolean isClear = false; //用于记录依稀
/***
* @param exp 算数表达式
* @return 根据表达式返回结果
*/
private String getRs(String exp){
Interpreter bsh = new Interpreter();
Number result = null;
try {
exp = filterExp(exp);
result = (Number)bsh.eval(exp);
} catch (EvalError e) {
e.printStackTrace();
isClear = true;
return "算数公式错误";
}
return result.doubleValue()+"";
}
/**
* @param exp 算数表达式
* @return 因为计算过程中,全程需要有小数参与.
*/
private String filterExp(String exp) {
String num[] = exp.split("");
String temp = null;
int begin=0,end=0;
for (int i = 1; i < num.length; i++) {
temp = num[i];
if(temp.matches("[+-/()*]")){
if(temp.equals(".")) continue;
end = i - 1;
temp = exp.substring(begin, end);
if(temp.trim().length() > 0 && temp.indexOf(".")<0)
num[i-1] = num[i-1]+".0";
begin = end + 1;
}
}
return Arrays.toString(num).replaceAll("[\\[\\], ]", "");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取对象引用
button_1=(Button) this.findViewById(R.id.button_1);
button_2=(Button) this.findViewById(R.id.button_2);
button_3=(Button) this.findViewById(R.id.button_3);
button_4=(Button) this.findViewById(R.id.button_4);
button_5=(Button) this.findViewById(R.id.button_5);
button_6=(Button) this.findViewById(R.id.button_6);
button_7=(Button) this.findViewById(R.id.button_7);
button_8=(Button) this.findViewById(R.id.button_8);
button_9=(Button) this.findViewById(R.id.button_9);
button_0=(Button) this.findViewById(R.id.button_0);
button_dot=(Button) this.findViewById(R.id.button_dot);
button_ADD=(Button) this.findViewById(R.id.button_ADD);
button_SUB=(Button) this.findViewById(R.id.button_SUB);
button_MUL=(Button) this.findViewById(R.id.button_MUL);
button_DEV=(Button) this.findViewById(R.id.button_DEV);
button_EQU=(Button) this.findViewById(R.id.button_EQU);
button_DEL=(Button) this.findViewById(R.id.button_DELETE);
button_XX=(Button) this.findViewById(R.id.button_XX);
button_CE=(Button) this.findViewById(R.id.button_CE);
edittext=(EditText) this.findViewById(R.id.editText1);
textview=(TextView) this.findViewById(R.id.textView1);
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="1";
edittext.setText(password);
edittext.setSelection(password.length());//将光标定位到最右边
// textview.setText(password);
}
});
button_2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
password+="2";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="3";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="4";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="5";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="6";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="7";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="8";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="9";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="0";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+=".";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_ADD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="+";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_SUB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="-";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_MUL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password+="×";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_DEV.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
password+="÷";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_EQU.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//计算
//-----------------------------------------------------
String exp=password;
exp = exp.replaceAll("×", "*");
exp = exp.replaceAll("÷", "/");
// textview.setText(exp);
if(exp==null || exp.trim().length()==0)
return;
exp = getRs(exp);
textview.setText(exp);
isClear = false;
//-----------------------------------------------------
}
});
button_CE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password="";
edittext.setText(password);
textview.setText("");
}
});
button_DEL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(password.length()>0){
password=password.substring(0,password.length()-1);
}
// else {
// password=password;
// }
edittext.setText(password);
edittext.setSelection(password.length());
}
});
button_XX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
password="("+password+")"+"×"+"("+password+")";
edittext.setText(password);
edittext.setSelection(password.length());
}
});
}
}
4.最后得到的程序运行:
程序中其实还有不完善的地方:
1.对于程序中使用String类型来处理得到按键字符,对每一次的操作是在字符串后面进行+操作,对于String字符串拼接性能并不高
每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以现阶段对于经常变化的字符串建议使用Stringbuilder。
2.对于计算还有不精确的地方(先还没有进行优化)。
大家可以下载玩玩:http://pan.baidu.com/s/1slm8T1F
Objective-C 支持的类型有两种:基本类型和类。基本类型,如同 C 语言中的 int 类型一样,拿来就可以直接用。而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值。类的初始化,需用类自身的方法(类方法)。代码中所创建的对象,不用后,还得记着释放。NSNumber 是类,而 NSInteger 只不过是个基本类型。既然 NSInteger 是基本类型,那么,使用起来就非常简单。声明一个变量,NSInteger myInt;直接赋值就可以了。 比如:myInt = 8;谈到
记录调试cyclegan遇到的问题准备工作1.代码下载2 使用visdom1)http://localhost/ 页面能否出现2)端口转发是否设置正确3)visdom是否配置完全准备工作小码农终于开始调代码了,准备深度调研cyclegan和GAN领域的网络。在这里记录准备过程中遇到的问题1.代码下载github下载速度非常慢,下载的包里有文件缺失,git clone失败方法1https://githubd.com 托管。将仓的地址复制至网页,个人感觉速度还是很慢。方法2修改本地hosts,
一、连接rabbit public Connection getConnection() { try { Connection connection = null; //定义一个连接工厂 ConnectionFactory factory = new ConnectionFacto...
Ceph当前的自动化部署有两个,分别是ceph-deploy和ceph-ansible,语言都是python,对应的github库地址为:ceph-deploy: https://github.com/ceph/ceph-deployceph-ansible: https://github.com/ceph/ceph-ansibleceph-deploy在手动部署中用的比较多,要实现自...
函数是在一个程序中可以被重复使用的一段程序。======================================普通函数函数名(必需)参数:形参(形式参数,定义的时候)、实参(实际参数,调用的时候)语句块(必需)return变量可以直接执行print,没有返回值。也可以利用return对语句块的运行结果进行返回。def learn_python(location):...
uojdescription给出\(n\)个变换,第\(i\)个变换是将区间中\(l_i,r_i\)的数\(x\)变成\((a_ix+b_i)\mod m\)。每次会新增一个变换,或者查询询问如果进行编号\([s,t]\)的操作,第\(k\)个数会变成多少。\(n\le10^5,q\le6\times10^5\)sol二进制分组。按顺序把变化插入线段树,如果线段树的某个满了...
介绍两种vtkImageActor和vtkImageViewer2
前言在如今各种App模式如出一辙、各个程序员们却各式各样花式实现效果的环境下,每接手一个项目,就能看到项目代码又不一样,分分钟想统一。同时为了加速后续新应用的开发,封装搭建了这么一个初步的简易框架,实现两分钟快速无脑构建首页。话不多说,先看实现的效果:简介实现功能:tab+fragment模式首页搭建支持ViewPager支持小红点展示(可选是否展示数量)支持突出展示某一个tab动态隐藏tab使用导入库implementation 'com.moore.lib:homeTabM
Python Flask No.4_Flask与HTML的结合1.在pycharm的flask项目中的templates中新建好html的界面2.在app.py中第一行from flask import Flask后面加上,render_template3.将app.py中原本的格式改成@app.route('/')def index(): return render_templ...
现在的Android手机双核、四核变得非常普遍,同时CPU频率经常轻松上2G,功耗肯定会显著增加。而大多数的ARM架构的CPU采用的是对称多处理(SMP)的方式处理多CPU。这就意味着每个CPU核心是被平等对待的,同时打开又同时关闭。显然,这样的做法在Mobile Device上显得很耗能。所以,Qualcomm的Snapdragon CPU使用了一种叫非对称多处理(aSMP)的技术,每个CPU核
目录uefi和legacy是两种不同的引导方式,扩展资料传统的legacy模式安装双系统查看电脑是32位还是64位u盘安装ubuntu重启、安装安装主要分为以下几步:亲测dell笔记本UEFI启动模式的电脑安装Win10和Ubuntu双系统Ubuntu 更换国内源uefi和legacy是两种不同的引导方式,uefi是新式的BIOS,legacy是传统BI...
首先我们从基础JS的执行机制去逐步开始理解...1-JS是单线程语言2-JS的Event Loop是JS的执行机制。深入了解JS的执行,就等于深入了解JS里的event loopJS为什么是单线程的???场景描述:现在有2个线程,process1 process2,由于是多线程的JS,所以他们对同一个dom,同时进行操作process1 删除了该dom,而process2 编辑了该dom,同时下达2个矛盾的命令,浏览器究竟该如何执行呢?JS为什么需要异步???......