android简单的计算器有错,求好心人帮忙(80分)

小白晒太阳 2012-08-28 10:21:54
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>
...全文
128 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小白晒太阳 2012-08-29
  • 打赏
  • 举报
回复
恩,可以。请问全屏那个fill_parent为什么这么写不能实现想要的效果?[Quote=引用 6 楼 的回复:]

EditText不变化是设置MaxLines属性为1,或者Lines为1,即最多为1行
[/Quote]
小白晒太阳 2012-08-29
  • 打赏
  • 举报
回复
光这样是不行的,我试过的,这样打个比方就是把容器设置成全屏的了,那些button控件是不变的[Quote=引用 5 楼 的回复:]

全屏
<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="fil……
[/Quote]
manoel 2012-08-29
  • 打赏
  • 举报
回复
最好把整个工程发上来看看。
wintergoes 2012-08-29
  • 打赏
  • 举报
回复
EditText不变化是设置MaxLines属性为1,或者Lines为1,即最多为1行

csdn_2013 2012-08-29
  • 打赏
  • 举报
回复
全屏
<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不随内容的变化而变化 这句话的意思能否具体点 什么内容的变化?
小白晒太阳 2012-08-29
  • 打赏
  • 举报
回复
我把properties中1.5改成1.6就好了,其实我发的代码有很多错误,我早上改好了。现在就是布局的问题,请问如何实现全屏显示并且EditText不随内容的变化而变化?大哥,帮忙回答一下,解决了就马上结贴了。[Quote=引用 2 楼 的回复:]

这么多代码看不过来,要看看Logcat里显示哪里报错就轻松了
[/Quote]
小白晒太阳 2012-08-29
  • 打赏
  • 举报
回复
我把properties中1.5改成1.6就好了,其实我发的代码有很多错误,我早上改好了。现在就是布局的问题,请问如何实现全屏显示并且EditText不随内容的变化而变化?大哥,帮忙回答一下,解决了就马上结贴了。
[Quote=引用楼主 的回复:]
Java code
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 a……
[/Quote]
  • 打赏
  • 举报
回复
这么多代码看不过来,要看看Logcat里显示哪里报错就轻松了
wintergoes 2012-08-29
  • 打赏
  • 举报
回复
按钮都全全屏的的XML代码



<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>
wintergoes 2012-08-28
  • 打赏
  • 举报
回复
我运行了一下没事,楼主把logCat里的日志发上来

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧