package com.example.calcultor;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;
public class MainActivity extends Activity implements OnClickListener{
static String number1=null;
static String number2=null;
static String number3=null;//实现连等功能
static double result=0;
static Boolean checkNum=false;
static Boolean check_ls=false;//实现连等功能
static int op=0;
private Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
private Button btn_multiply,btn_divide,btn_dot,btn_clear,btn_plus,btn_reduce,btn_result;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0=(Button)findViewById(R.id.zero);
btn1=(Button)findViewById(R.id.one);
btn2=(Button)findViewById(R.id.two);
btn3=(Button)findViewById(R.id.three);
btn4=(Button)findViewById(R.id.four);
btn5=(Button)findViewById(R.id.five);
btn6=(Button)findViewById(R.id.six);
btn7=(Button)findViewById(R.id.seven);
btn8=(Button)findViewById(R.id.eight);
btn9=(Button)findViewById(R.id.nine);
btn_reduce=(Button)findViewById(R.id.reduce);
btn_multiply=(Button)findViewById(R.id.multiply);
btn_divide=(Button)findViewById(R.id.divide);
btn_dot=(Button)findViewById(R.id.dot);
btn_clear=(Button)findViewById(R.id.clear);
btn_plus=(Button)findViewById(R.id.plus);
btn_result=(Button)findViewById(R.id.result);
et=(EditText)findViewById(R.id.et);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn0.setOnClickListener(this);
btn_reduce.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_dot.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_result.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch(v.getId()){
default:
break;
case R.id.one:setTextValue(String.valueOf(R.id.one));
break;
case R.id.two:setTextValue(String.valueOf(R.id.two));
break;
case R.id.three:setTextValue(String.valueOf(R.id.three));
break;
case R.id.four:setTextValue(String.valueOf(R.id.four));
break;
case R.id.five:setTextValue(String.valueOf(R.id.five));
break;
case R.id.six:setTextValue(String.valueOf(R.id.six));
break;
case R.id.seven:setTextValue(String.valueOf(R.id.seven));
break;
case R.id.eight:setTextValue(String.valueOf(R.id.eight));
break;
case R.id.nine:setTextValue(String.valueOf(R.id.nine));
break;
case R.id.zero:
if(et.getText().toString()!=null)
setTextValue(String.valueOf(R.id.zero));
else
break;
case R.id.plus:{op=1;get_number();
break;}
case R.id.reduce:{op=2;get_number();
break;}
case R.id.multiply:{op=3;get_number();
break;}
case R.id.divide:{op=4;get_number();
break;}
case R.id.dot:
if(et.getText().toString()!=null)
setTextValue(String.valueOf(R.id.dot));
else
break;
case R.id.clear:All_clear();
break;
case R.id.result:print_result();
break;
}
}
private void setTextValue(String str){
et.append(str);
}
private void get_number(){
checkNum=!checkNum;
if(checkNum){
number1=et.getText().toString();
et.setText("");
}
else number2=et.getText().toString();
}
private void All_clear(){
number1="";
number2="";
number3="";//实现连等功能
result=0;
checkNum=false;
check_ls=false;//实现连等功能
op=0;
}
private void print_result(){
if(!check_ls){
switch(op){
case 1:result=Double.valueOf(number1)+Double.valueOf(number2);break;
case 2:result=Double.valueOf(number1)-Double.valueOf(number2);break;
case 3:result=Double.valueOf(number1)*Double.valueOf(number2);break;
case 4:result=Double.valueOf(number1)/Double.valueOf(number2);break;
}
check_ls=true;
}
else {
switch(op){
case 1:result+=Double.valueOf(number3);break;
case 2:result-=Double.valueOf(number3);break;
case 3:result*=Double.valueOf(number3);break;
case 4:result/=Double.valueOf(number3);break;
}
et.setText(String.valueOf(result));
}
}
}
代码有错误,整个程序运行到android虚拟机上就出错,运行不了。我是菜鸟,所以希望大家给点帮助,真心先谢谢各位花个时间看看我的代码错在哪些地方了。XML和strings里面应该没有错误,因为不写上面代码程序是可以运行的,且布局显示正确。不过我也把贴出来。<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow>
<EditText
android:hint="@string/input"
android:id="@+id/et"
android:layout_span="4"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<Button
android:text="@string/one"
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/two"
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/three"
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/plus"
android:id="@+id/plus"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/four"
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/five"
android:id="@+id/five"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/six"
android:id="@+id/six"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/reduce"
android:id="@+id/reduce"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/seven"
android:id="@+id/seven"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/eight"
android:id="@+id/eight"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/nine"
android:id="@+id/nine"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/multiply"
android:id="@+id/multiply"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/zero"
android:id="@+id/zero"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/divide"
android:id="@+id/divide"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/dot"
android:id="@+id/dot"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/result"
android:id="@+id/result"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/clear"
android:id="@+id/clear"
android:layout_height="50dp"
android:layout_span="4"/>
</TableRow>
</TableLayout><resources> <string name="app_name">Calcultor</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="zero">0</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="plus">+</string>
<string name="reduce">-</string>
<string name="multiply">*</string>
<string name="divide">/</string>
<string name="dot">.</string>
<string name="clear">清除</string>
<string name="result">=</string>
<string name="input">请输入</string>
</resources>
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;
public class MainActivity extends Activity implements OnClickListener{
static String number1=null;
static String number2=null;
static String number3=null;//实现连等功能
static double result=0;
static Boolean checkNum=false;
static Boolean check_ls=false;//实现连等功能
static int op=0;
private Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
private Button btn_multiply,btn_divide,btn_dot,btn_clear,btn_plus,btn_reduce,btn_result;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0=(Button)findViewById(R.id.zero);
btn1=(Button)findViewById(R.id.one);
btn2=(Button)findViewById(R.id.two);
btn3=(Button)findViewById(R.id.three);
btn4=(Button)findViewById(R.id.four);
btn5=(Button)findViewById(R.id.five);
btn6=(Button)findViewById(R.id.six);
btn7=(Button)findViewById(R.id.seven);
btn8=(Button)findViewById(R.id.eight);
btn9=(Button)findViewById(R.id.nine);
btn_reduce=(Button)findViewById(R.id.reduce);
btn_multiply=(Button)findViewById(R.id.multiply);
btn_divide=(Button)findViewById(R.id.divide);
btn_dot=(Button)findViewById(R.id.dot);
btn_clear=(Button)findViewById(R.id.clear);
btn_plus=(Button)findViewById(R.id.plus);
btn_result=(Button)findViewById(R.id.result);
et=(EditText)findViewById(R.id.et);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn0.setOnClickListener(this);
btn_reduce.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_dot.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_result.setOnClickListener(this);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
switch(v.getId()){
default:
break;
case R.id.one:setTextValue(String.valueOf(R.id.one));
break;
case R.id.two:setTextValue(String.valueOf(R.id.two));
break;
case R.id.three:setTextValue(String.valueOf(R.id.three));
break;
case R.id.four:setTextValue(String.valueOf(R.id.four));
break;
case R.id.five:setTextValue(String.valueOf(R.id.five));
break;
case R.id.six:setTextValue(String.valueOf(R.id.six));
break;
case R.id.seven:setTextValue(String.valueOf(R.id.seven));
break;
case R.id.eight:setTextValue(String.valueOf(R.id.eight));
break;
case R.id.nine:setTextValue(String.valueOf(R.id.nine));
break;
case R.id.zero:
if(et.getText().toString()!=null)
setTextValue(String.valueOf(R.id.zero));
else
break;
case R.id.plus:{op=1;get_number();
break;}
case R.id.reduce:{op=2;get_number();
break;}
case R.id.multiply:{op=3;get_number();
break;}
case R.id.divide:{op=4;get_number();
break;}
case R.id.dot:
if(et.getText().toString()!=null)
setTextValue(String.valueOf(R.id.dot));
else
break;
case R.id.clear:All_clear();
break;
case R.id.result:print_result();
break;
}
}
private void setTextValue(String str){
et.append(str);
}
private void get_number(){
checkNum=!checkNum;
if(checkNum){
number1=et.getText().toString();
et.setText("");
}
else number2=et.getText().toString();
}
private void All_clear(){
number1="";
number2="";
number3="";//实现连等功能
result=0;
checkNum=false;
check_ls=false;//实现连等功能
op=0;
}
private void print_result(){
if(!check_ls){
switch(op){
case 1:result=Double.valueOf(number1)+Double.valueOf(number2);break;
case 2:result=Double.valueOf(number1)-Double.valueOf(number2);break;
case 3:result=Double.valueOf(number1)*Double.valueOf(number2);break;
case 4:result=Double.valueOf(number1)/Double.valueOf(number2);break;
}
check_ls=true;
}
else {
switch(op){
case 1:result+=Double.valueOf(number3);break;
case 2:result-=Double.valueOf(number3);break;
case 3:result*=Double.valueOf(number3);break;
case 4:result/=Double.valueOf(number3);break;
}
et.setText(String.valueOf(result));
}
}
}
代码有错误,整个程序运行到android虚拟机上就出错,运行不了。我是菜鸟,所以希望大家给点帮助,真心先谢谢各位花个时间看看我的代码错在哪些地方了。XML和strings里面应该没有错误,因为不写上面代码程序是可以运行的,且布局显示正确。不过我也把贴出来。<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TableRow>
<EditText
android:hint="@string/input"
android:id="@+id/et"
android:layout_span="4"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow>
<Button
android:text="@string/one"
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/two"
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/three"
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/plus"
android:id="@+id/plus"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/four"
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/five"
android:id="@+id/five"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/six"
android:id="@+id/six"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/reduce"
android:id="@+id/reduce"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/seven"
android:id="@+id/seven"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/eight"
android:id="@+id/eight"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/nine"
android:id="@+id/nine"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/multiply"
android:id="@+id/multiply"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/zero"
android:id="@+id/zero"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/divide"
android:id="@+id/divide"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/dot"
android:id="@+id/dot"
android:layout_width="50dp"
android:layout_height="50dp"/>
<Button
android:text="@string/result"
android:id="@+id/result"
android:layout_width="50dp"
android:layout_height="50dp"/>
</TableRow>
<TableRow>
<Button
android:text="@string/clear"
android:id="@+id/clear"
android:layout_height="50dp"
android:layout_span="4"/>
</TableRow>
</TableLayout><resources> <string name="app_name">Calcultor</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="zero">0</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="plus">+</string>
<string name="reduce">-</string>
<string name="multiply">*</string>
<string name="divide">/</string>
<string name="dot">.</string>
<string name="clear">清除</string>
<string name="result">=</string>
<string name="input">请输入</string>
</resources>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>EditText不随内容的变化而变化 这句话的意思能否具体点 什么内容的变化?
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" ><TableRow><EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="4"
android:layout_weight="1"
android:hint="@string/input"
android:lines="1"
android:maxLines="1" /></TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:text="@string/one"
android:id="@+id/one"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/two"
android:id="@+id/two"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/three"
android:id="@+id/three"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/plus"
android:id="@+id/plus"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:text="@string/four"
android:id="@+id/four"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/five"
android:id="@+id/five"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/six"
android:id="@+id/six"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/reduce"
android:id="@+id/reduce"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:text="@string/seven"
android:id="@+id/seven"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/eight"
android:id="@+id/eight"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/nine"
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/multiply"
android:id="@+id/multiply"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:text="@string/zero"
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/divide"
android:id="@+id/divide"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/dot"
android:id="@+id/dot"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<Button
android:text="@string/result"
android:id="@+id/result"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1" ><Button
android:id="@+id/clear"
android:layout_height="match_parent"
android:layout_span="4"
android:layout_weight="1"
android:text="@string/clear" /></TableRow>
</TableLayout>