我正在制作触控计算器。每当我运行应用程序时,它在第一次工作正常但在使用1次后我尝试按任何数字,如1,2,3等。它只按一次。那就是我不能写超过1个图号。这是我的代码。
package bt4u.com.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20;
float result;
float a;
float b;
float c;
Boolean get=false,error=false;
int d,count=0,q;
TextView tv,tv2;
String str;
int action;//0-add,1-sub,2-mul,3-div
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
b6=(Button)findViewById(R.id.button6);
b7=(Button)findViewById(R.id.button7);
b8=(Button)findViewById(R.id.button8);
b9=(Button)findViewById(R.id.button9);
b10=(Button)findViewById(R.id.button10);
b11=(Button)findViewById(R.id.button11);
b12=(Button)findViewById(R.id.button12);
b13=(Button)findViewById(R.id.button13);
b14=(Button)findViewById(R.id.button14);
b15=(Button)findViewById(R.id.button15);
b16=(Button)findViewById(R.id.button16);
b17=(Button)findViewById(R.id.button17);
b18=(Button)findViewById(R.id.button18);
b19=(Button)findViewById(R.id.button19);
b20=(Button)findViewById(R.id.button20);
tv=(TextView)findViewById(R.id.textView2);
tv2=(TextView)findViewById(R.id.textView);
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"9");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button2:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"8");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button3:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"7");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button4:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"6");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button5:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"5");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button6:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"4");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button7:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"3");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button8:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"2");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button9:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"1");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button10:
str=tv.getText().toString();
if (str.length()!=0) {
if (str.contains("."))
q=0;
else
tv.setText(tv.getText() + ".");
get=false;
}
break;
case R.id.button11:
if (get)
tv.setText(null);
tv.setText(tv.getText()+"0");
b=Float.parseFloat(tv.getText().toString());
break;
case R.id.button12:
switch (action){
case 0:
result=a+b;
break;
case 1:
result=a-b;
break;
case 2:
result=a*b;
break;
case 3:
result=a/b;
break;
}
if (result==Math.ceil(result)) {
d = (int) result;
tv.setText("" + d);
}
else {
tv.setText("" + result);
}
tv2.setText(null);
get=true;
break;
case R.id.button13:
try {
a = Float.parseFloat(tv.getText().toString());
}
catch (NumberFormatException e){
error=true;
}
if (!error) {
tv2.setText(tv.getText() + "+");
action = 0;
}
error = false;
tv.setText(null);
break;
case R.id.button14:
try {
a = Float.parseFloat(tv.getText().toString());
}
catch (NumberFormatException e){
error=true;
}
if (!error) {
tv2.setText(tv.getText() + "*");
action = 2;
}
error = false;
tv.setText(null);
break;
case R.id.button15:
try {
a = Float.parseFloat(tv.getText().toString());
}
catch (NumberFormatException e){
error=true;
}
if (!error) {
tv2.setText(tv.getText() + "-");
action = 1;
}
error = false;
tv.setText(null);
break;
case R.id.button16:
try {
a = Float.parseFloat(tv.getText().toString());
}
catch (NumberFormatException e){
error=true;
}
if (!error) {
tv2.setText(tv.getText() + "/");
action = 3;
}
error = false;
tv.setText(null);
break;
}
}
};
b1.setOnClickListener(click); b14.setOnClickListener(click);
b2.setOnClickListener(click); b15.setOnClickListener(click);
b3.setOnClickListener(click); b16.setOnClickListener(click);
b4.setOnClickListener(click); b17.setOnClickListener(click);
b5.setOnClickListener(click); b18.setOnClickListener(click);
b6.setOnClickListener(click); b19.setOnClickListener(click);
b7.setOnClickListener(click); b20.setOnClickListener(click);
b8.setOnClickListener(click); b13.setOnClickListener(click);
b9.setOnClickListener(click); b12.setOnClickListener(click);
b10.setOnClickListener(click); b11.setOnClickListener(click);
}
}
答案 0 :(得分:0)
您已将get
和error
个变量定义为全局变量,并且每次全局变更时都可能会阻止对下次点击的进一步操作。在onClick
方法中将它们定义为局部变量。
答案 1 :(得分:0)
case R.id.button12:
switch (action){
case 0:
result=a+b;
break;
case 1:
result=a-b;
break;
case 2:
result=a*b;
break;
case 3:
result=a/b;
break;
}
if (result==Math.ceil(result)) {
d = (int) result;
tv.setText("" + d);
}
else {
tv.setText("" + result);
}
tv2.setText(null);
get=false;
break;
//仅更改案例R.id.button12 .......我希望,你可以解决你的问题......