嘛,学习android已经一星期了,第一次做出小软件还是有点小兴奋的。介绍下我的代码吧
其中关于Button和 Listener的声明神马的,还有ListenerEqu 部分来自这里的代码http://blog.csdn.net/nupt123456789/article/details/7832443 非常感谢这位的代码给我的帮助。。 这个小计算器其他部分都是我自己编的- -,可能有点问题。各位跟我一样的初学者就看看就好了~我感觉蛮好懂的,没有什么复杂的调用。
计算器的负数还没弄,就不弄了~看看其他的部分就好了 不说了 贴代码~希望大家多多支持,这是本人安卓开发第一步~
package com.example.calculate1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.lang.*;
import com.example.calculate1.R.string;
public class MainActivity extends Activity {
Button btn_num1;
Button btn_num2;
Button btn_num3;
Button btn_num4;
Button btn_num5;
Button btn_num6;
Button btn_num7;
Button btn_num8;
Button btn_num9;
Button btn_num0;
Button btnEqu;
Button btnAdd;
Button btnSub;
Button btnMul;
Button btnDiv;
Button btnPoint;
Button btnCln;
OnClickListener listener0=null;
OnClickListener listener1=null;
OnClickListener listener2=null;
OnClickListener listener3=null;
OnClickListener listener4=null;
OnClickListener listener5=null;
OnClickListener listener6=null;
OnClickListener listener7=null;
OnClickListener listener8=null;
OnClickListener listener9=null;
OnClickListener listenerMul=null;
OnClickListener listenerDiv=null;
OnClickListener listenerSub=null;
OnClickListener listenerAdd=null;
OnClickListener listenerPoint=null;
OnClickListener listenerCln=null;
OnClickListener listenerEqu=null;
EditText displayText;
double num1=0;
double num2=0;
double result=0;
String myString;
int calType = 0;
String b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayText=(EditText)findViewById(R.id.text);
btn_num0 = (Button)findViewById(R.id.zero);
btn_num1 = (Button)findViewById(R.id.one);
btn_num2 = (Button)findViewById(R.id.two);
btn_num3 = (Button)findViewById(R.id.three);
btn_num4 = (Button)findViewById(R.id.four);
btn_num5 = (Button)findViewById(R.id.five);
btn_num6 = (Button)findViewById(R.id.six);
btn_num7 = (Button)findViewById(R.id.seven);
btn_num8 = (Button)findViewById(R.id.eight);
btn_num9 = (Button)findViewById(R.id.nine);
btnAdd = (Button)findViewById(R.id.plus);
btnMul = (Button)findViewById(R.id.multiple);
btnSub = (Button)findViewById(R.id.subtraction);
btnDiv = (Button)findViewById(R.id.division);
btnPoint = (Button)findViewById(R.id.point);
btnCln = (Button)findViewById(R.id.clean);
btnEqu=(Button)findViewById(R.id.equail);
listener0 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "0";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="0";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener1 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "1";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="1";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener2 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "2";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="2";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener3 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "3";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="3";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener4 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "4";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="4";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener5 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "5";
String b =myString ;
myString =b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="5";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener6 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "6";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="6";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener7 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "7";
String b =myString ;
myString =b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="7";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener8 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "8";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="8";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listener9 = new OnClickListener(){
public void onClick(View v){
if(num1==0){
String a= "9";
String b =myString ;
myString = b+a;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
else{
String c="9";
String d= myString ;
myString =d+c;
myString=myString.replaceAll("n","");
myString= myString.replaceAll("u","");
myString= myString.replaceAll("l","");
}
displayText.setText(myString);
}
};
listenerPoint = new OnClickListener(){
public void onClick(View v){
String b =myString ;
myString=".";
myString = b+myString;
displayText.setText(myString);
}
};
listenerAdd = new OnClickListener(){
public void onClick(View v){
num1=Double.parseDouble(myString);
calType=1;
myString=null;
b=null;
}
};
listenerSub = new OnClickListener(){
public void onClick(View v){
num1=Double.parseDouble(myString);
calType=2;
myString=null;
b=null;
}
};
listenerMul = new OnClickListener(){
public void onClick(View v){
num1=Double.parseDouble(myString);
calType=3;
myString=null;
b=null;
}
};
listenerDiv = new OnClickListener(){
public void onClick(View v){
num1=Double.parseDouble(myString);
calType=4;
myString=null;
b=null;
}
};
listenerEqu = new OnClickListener(){
public void onClick(View v){
CharSequence temp = displayText.getText();
String myString = temp.toString();
num2=Double.parseDouble(myString);
switch(calType){
case 0:
result=num2;
break;
case 1:
result=num1+num2;
break;
case 2:
result=num1-num2;
break;
case 3:
result=num1*num2;
break;
case 4:
result=num1/num2;
break;
default:
result=0;
break;
}
String temp_result = String.valueOf(result);
displayText.setText(temp_result);
myString=null;
b=null;
}
};
listenerCln = new OnClickListener(){
public void onClick(View v){
String temp_result = String.valueOf(result);
myString=temp_result;
num1=0;
num2=0;
displayText.setText("0");
myString="null";
b="null";
}
};
btn_num0.setOnClickListener(listener0);
btn_num1.setOnClickListener(listener1);
btn_num2.setOnClickListener(listener2);
btn_num3.setOnClickListener(listener3);
btn_num4.setOnClickListener(listener4);
btn_num5.setOnClickListener(listener5);
btn_num6.setOnClickListener(listener6);
btn_num7.setOnClickListener(listener7);
btn_num8.setOnClickListener(listener8);
btn_num9.setOnClickListener(listener9);
btnAdd.setOnClickListener(listenerAdd);
btnSub.setOnClickListener(listenerSub);
btnCln.setOnClickListener(listenerCln);
btnDiv.setOnClickListener(listenerDiv);
btnMul.setOnClickListener(listenerMul);
btnPoint.setOnClickListener(listenerPoint);
btnEqu.setOnClickListener(listenerEqu);
}
protected String concat(String myString2, String a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
上面的是Activity 下面的是Layout
其中Layout 这面之前用gridView 实验了下 虽然能把按键布置的很好,但是不知道怎么对这些按键进行编辑。所以只能一个个Button编辑
还有之前用了很多replaceAll 是因为 我想要实现可以输入多位数的数字进行计算,用的是字符串相互连接在一起的方法,而myString的初始值是NULL (这东西真烦,搞了我好久) 所以得用replaceAll 吧NULL去掉,差不多就这些吧~如果有点小问题应该也不是很严重~谢谢支持哈
对了 布局用的还不熟练- - 按键的排列可能有点难看 先这样吧~我要开始我的第二个小软件了,哈哈,做安卓软件还是很开心的,很有成就感