这段代码有很多不明白处,请大仙们讲解讲解,帮我学习Android,尽量详细点,必将感激不尽!
package android.text;import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;public class CalculatorActivity extends Activity {
/** Called when the activity is first created. */
String str="";
EditText et;
int c=0,flag=0;
double b=0.0,g=0.0,f=0.0; //???
View vi; //???
///这是监听么
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "退出");
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){finish();}
return super.onOptionsItemSelected(item);
}
//计算方法
public double calculater(){
switch(c){
case 0:f=g;break;//??case 0什么作用
case 1:f=b+g;break;
case 2:f=b-g;break;
case 3:f=b*g;break;
case 4:f=b/g;break;
}
b=f;//??
c=0;
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得按键
final Button number[]=new Button[10];
final Button fuhao[]=new Button[11];
fuhao[0]=(Button)findViewById(R.id.button01);
fuhao[1]=(Button)findViewById(R.id.button02);
fuhao[2]=(Button)findViewById(R.id.button03);
fuhao[3]=(Button)findViewById(R.id.button04);
fuhao[4]=(Button)findViewById(R.id.button05);
fuhao[5]=(Button)findViewById(R.id.button06);
fuhao[6]=(Button)findViewById(R.id.buttonce);
fuhao[7]=(Button)findViewById(R.id.buttonc);
fuhao[8]=(Button)findViewById(R.id.zheng);
fuhao[9]=(Button)findViewById(R.id.kaifang);
fuhao[10]=(Button)findViewById(R.id.pingfang);
number[0]=(Button)findViewById(R.id.button0);
number[1]=(Button)findViewById(R.id.button1);
number[2]=(Button)findViewById(R.id.button2);
number[3]=(Button)findViewById(R.id.button3);
number[4]=(Button)findViewById(R.id.button4);
number[5]=(Button)findViewById(R.id.button5);
number[6]=(Button)findViewById(R.id.button6);
number[7]=(Button)findViewById(R.id.button7);
number[8]=(Button)findViewById(R.id.button8);
number[9]=(Button)findViewById(R.id.button9);
et=(EditText) findViewById(R.id.textView1);
et.setText(str);
//ce
fuhao[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
str=""+0;
et.setText(str);
vi=v; }
});
//c
fuhao[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int lg=str.length();
if(lg==0)
{
return;
}
else
{
str=str.substring(0, str.length()-1);
et.setText(str);
}
}
});
///这一段解释一下
//+/-
fuhao[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(vi!=fuhao[5]&&str!=""){
char ch=str.charAt(0);
if(ch=='-')
str=str.replace("-","");
else
str="-"+str;
et.setText(str);
}
}
});
//开方
fuhao[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=Math.sqrt(a)+"";
et.setText(str);
}}
});
//平方
fuhao[10].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=""+a*a;
et.setText(str);
}}
}); //设定数字按键
number[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=0;
et.setText(str);
flag=0;
}
else{
char ch1[];
ch1=str.toCharArray();
if(!(ch1.length==1&&ch1[0]=='0'))
{str+=0;
et.setText(str);}
}
vi=v;
}
});
///number1解释一下if else
number[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){ ///flag==1什么意思
str="";
str+=1;
et.setText(str);
flag=0;
}
else{
str+=1;
et.setText(str);
}
vi=v;
}
}); number[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=2;
et.setText(str);
flag=0;
}
else{
str+=2;
et.setText(str);
}
vi=v;
}
}); number[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=3;
et.setText(str);
flag=0;
}
else{
str+=3;
et.setText(str);
}
vi=v;
}
}); number[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=4;
et.setText(str);
flag=0;
}
else{
str+=4;
et.setText(str);
}
vi=v;
}
}); number[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=5;
et.setText(str);
flag=0;
}
else{
str+=5;
et.setText(str);
}
vi=v;
}
}); number[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=6;
et.setText(str);
flag=0;
}
else{
str+=6;
et.setText(str);
}
vi=v;
}
}); number[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=7;
et.setText(str);
flag=0;
}
else{
str+=7;
et.setText(str);
}
vi=v;
}
}); number[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=8;
et.setText(str);
flag=0;
}
else{
str+=8;
et.setText(str);
}
vi=v;
}
}); number[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=9;
et.setText(str);
flag=0;
}
else{
str+=9;
et.setText(str);
}
vi=v;
}
}); //设定符号键 //////解释一下加运算
//加
fuhao[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=1;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=1;
flag=1;
vi=v;
}}
}
}); //减
fuhao[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=2;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=2;
flag=1;
vi=v;
}}
}
}); //乘
fuhao[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=3;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=3;
flag=1;
vi=v;
}}
}
}); //除
fuhao[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=4;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=4;
flag=1;
vi=v;
}}
}
});
//等号
fuhao[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""&&vi!=fuhao[0]&&vi!=fuhao[1]&&vi!=fuhao[2]&&vi!=fuhao[3]){
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
flag=1;
vi=v; }
}
});
//小数点
fuhao[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str==""){
str+=".";
et.setText(str);
}
else{
char ch1[];int x=0;
ch1=str.toCharArray();
for(int i=0;i<ch1.length;i++)
if(ch1[i]=='.')
x++;
if(x==0){
str+=".";
et.setText(str);
}
} }
});
}
}
package android.text;import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;public class CalculatorActivity extends Activity {
/** Called when the activity is first created. */
String str="";
EditText et;
int c=0,flag=0;
double b=0.0,g=0.0,f=0.0; //???
View vi; //???
///这是监听么
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "退出");
return super.onCreateOptionsMenu(menu);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){finish();}
return super.onOptionsItemSelected(item);
}
//计算方法
public double calculater(){
switch(c){
case 0:f=g;break;//??case 0什么作用
case 1:f=b+g;break;
case 2:f=b-g;break;
case 3:f=b*g;break;
case 4:f=b/g;break;
}
b=f;//??
c=0;
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得按键
final Button number[]=new Button[10];
final Button fuhao[]=new Button[11];
fuhao[0]=(Button)findViewById(R.id.button01);
fuhao[1]=(Button)findViewById(R.id.button02);
fuhao[2]=(Button)findViewById(R.id.button03);
fuhao[3]=(Button)findViewById(R.id.button04);
fuhao[4]=(Button)findViewById(R.id.button05);
fuhao[5]=(Button)findViewById(R.id.button06);
fuhao[6]=(Button)findViewById(R.id.buttonce);
fuhao[7]=(Button)findViewById(R.id.buttonc);
fuhao[8]=(Button)findViewById(R.id.zheng);
fuhao[9]=(Button)findViewById(R.id.kaifang);
fuhao[10]=(Button)findViewById(R.id.pingfang);
number[0]=(Button)findViewById(R.id.button0);
number[1]=(Button)findViewById(R.id.button1);
number[2]=(Button)findViewById(R.id.button2);
number[3]=(Button)findViewById(R.id.button3);
number[4]=(Button)findViewById(R.id.button4);
number[5]=(Button)findViewById(R.id.button5);
number[6]=(Button)findViewById(R.id.button6);
number[7]=(Button)findViewById(R.id.button7);
number[8]=(Button)findViewById(R.id.button8);
number[9]=(Button)findViewById(R.id.button9);
et=(EditText) findViewById(R.id.textView1);
et.setText(str);
//ce
fuhao[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
str=""+0;
et.setText(str);
vi=v; }
});
//c
fuhao[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int lg=str.length();
if(lg==0)
{
return;
}
else
{
str=str.substring(0, str.length()-1);
et.setText(str);
}
}
});
///这一段解释一下
//+/-
fuhao[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(vi!=fuhao[5]&&str!=""){
char ch=str.charAt(0);
if(ch=='-')
str=str.replace("-","");
else
str="-"+str;
et.setText(str);
}
}
});
//开方
fuhao[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=Math.sqrt(a)+"";
et.setText(str);
}}
});
//平方
fuhao[10].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=""+a*a;
et.setText(str);
}}
}); //设定数字按键
number[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=0;
et.setText(str);
flag=0;
}
else{
char ch1[];
ch1=str.toCharArray();
if(!(ch1.length==1&&ch1[0]=='0'))
{str+=0;
et.setText(str);}
}
vi=v;
}
});
///number1解释一下if else
number[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){ ///flag==1什么意思
str="";
str+=1;
et.setText(str);
flag=0;
}
else{
str+=1;
et.setText(str);
}
vi=v;
}
}); number[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=2;
et.setText(str);
flag=0;
}
else{
str+=2;
et.setText(str);
}
vi=v;
}
}); number[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=3;
et.setText(str);
flag=0;
}
else{
str+=3;
et.setText(str);
}
vi=v;
}
}); number[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=4;
et.setText(str);
flag=0;
}
else{
str+=4;
et.setText(str);
}
vi=v;
}
}); number[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=5;
et.setText(str);
flag=0;
}
else{
str+=5;
et.setText(str);
}
vi=v;
}
}); number[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=6;
et.setText(str);
flag=0;
}
else{
str+=6;
et.setText(str);
}
vi=v;
}
}); number[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=7;
et.setText(str);
flag=0;
}
else{
str+=7;
et.setText(str);
}
vi=v;
}
}); number[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=8;
et.setText(str);
flag=0;
}
else{
str+=8;
et.setText(str);
}
vi=v;
}
}); number[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=9;
et.setText(str);
flag=0;
}
else{
str+=9;
et.setText(str);
}
vi=v;
}
}); //设定符号键 //////解释一下加运算
//加
fuhao[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=1;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=1;
flag=1;
vi=v;
}}
}
}); //减
fuhao[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=2;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=2;
flag=1;
vi=v;
}}
}
}); //乘
fuhao[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=3;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=3;
flag=1;
vi=v;
}}
}
}); //除
fuhao[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){
c=4;
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
c=4;
flag=1;
vi=v;
}}
}
});
//等号
fuhao[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""&&vi!=fuhao[0]&&vi!=fuhao[1]&&vi!=fuhao[2]&&vi!=fuhao[3]){
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
flag=1;
vi=v; }
}
});
//小数点
fuhao[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str==""){
str+=".";
et.setText(str);
}
else{
char ch1[];int x=0;
ch1=str.toCharArray();
for(int i=0;i<ch1.length;i++)
if(ch1[i]=='.')
x++;
if(x==0){
str+=".";
et.setText(str);
}
} }
});
}
}
解决方案 »
- activitygroup不能接收横竖屏事件
- 哪位朋友能给我一段android方面使用OPengl ES绘制镜面反射的代码吗?
- 访问sd卡的疑问
- 如何读取SIm卡中的联系人
- android textView 自动滚动问题
- cocos2d-x 如何通过CCProgressTimer让血条跟随怪物移动?
- fragment 中弹出popwindow 中包含Spinner,Spinner可以正常显示数据,但一点就就报错。android.view.WindowMana
- 安卓蓝牙4.0连接成功后,discoverServices后没有进入回调函数
- android新SDK中的一个疑问
- expandableListView如何实现滑动时自动展开
- 如何隐式的设置当前的输入法
- android关于汉字 编码问题
fuhao[8].setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
if(vi!=fuhao[5]&&str!=""){
char ch=str.charAt(0);
if(ch=='-')
str=str.replace("-","");
else
str="-"+str;
et.setText(str);
}
}
});
这是一段加减符号判断,在字符串前面加个减号,如果前面是减号就去掉