gpt4 book ai didi

java - 为什么文本未显示在 textView_1 中且运算符不起作用

转载 作者:行者123 更新时间:2023-12-01 10:28:02 24 4
gpt4 key购买 nike

我正在为Android制作一个计算器。在添加代码以将文本显示到 textView_1 之前,一切都工作正常,但之后文本都不会显示在 textView_1 中,任何操作符都无法工作。

package com.example.anunay.basiccalculator1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.lang.*;

public class MainActivity extends AppCompatActivity {

Button button_0, button_1, button_2, button_3, button_4, button_5, button_6, button_7, button_8,
button_9, button_Deci, button_Equal, button_Add, button_Sub, button_Mul, button_Div,
button_Back, button_Expo, button_Sqrt, button_Clear;


TextView textView_1, textView_2;

float mValue1, mValue2;

// double mValue3;

String mString1, mString2, mString2_Rev, textView_1Value;

boolean mAddition = false, mSubtraction = false, mMultiplication = false, mDivision = false,
mEqualPressed = false, mSqrt = false, mExponential = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button_0 = (Button) findViewById(R.id.button0);
button_1 = (Button) findViewById(R.id.button1);
button_2 = (Button) findViewById(R.id.button2);
button_3 = (Button) findViewById(R.id.button3);
button_4 = (Button) findViewById(R.id.button4);
button_5 = (Button) findViewById(R.id.button5);
button_6 = (Button) findViewById(R.id.button6);
button_7 = (Button) findViewById(R.id.button7);
button_8 = (Button) findViewById(R.id.button8);
button_9 = (Button) findViewById(R.id.button9);
button_Deci = (Button) findViewById(R.id.buttonDeci);
button_Add = (Button) findViewById((R.id.buttonAdd));
button_Sub = (Button) findViewById(R.id.buttonSub);
button_Mul = (Button) findViewById(R.id.buttonMul);
button_Div = (Button) findViewById(R.id.buttonDiv);
button_Sqrt = (Button) findViewById(R.id.buttonSqrt);
button_Expo = (Button) findViewById(R.id.buttonExpo);
button_Back = (Button) findViewById(R.id.buttonBack);
button_Clear = (Button) findViewById(R.id.buttonClear);
button_Equal = (Button) findViewById(R.id.buttonEqual);

textView_2 = (TextView) findViewById(R.id.textView2);



button_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("0");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"0");
mEqualPressed = false;
}
});

button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("1");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"1");
mEqualPressed = false;
}
});

button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("2");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"2");
mEqualPressed = false;
}
});

button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("3");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"3");
mEqualPressed = false;
}
});

button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("4");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"4");
mEqualPressed = false;
}
});

button_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("5");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"5");
mEqualPressed = false;
}
});

button_6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("6");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"6");
mEqualPressed = false;
}
});

button_7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("7");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"7");
mEqualPressed = false;
}
});

button_8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("8");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"8");
mEqualPressed = false;
}
});

button_9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText("9");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+"9");
mEqualPressed = false;
}
});

button_Deci.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if(mEqualPressed) {
textView_2.setText(".");
textView_1.setText(" ");

}
else textView_2.setText(textView_2.getText()+".");
mEqualPressed = false;
}
});

button_Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textView_2.getText()==""){
textView_2.setText("");
}
else if (textView_2.getText()=="-")
textView_2.setText(" ");

else
{
textView_1.setText(textView_2.getText().toString()+"+");
mValue1 = Float.parseFloat(textView_2.getText() + "");
mAddition = true;
textView_2.setText(null);
mEqualPressed = false;}
}
});

button_Sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (textView_2.getText()==""){
textView_2.setText("-");
}
else if (textView_2.getText()=="-")
textView_2.setText("-");
else{
textView_1.setText(textView_2.getText().toString()+"-");
mValue1 = Float.parseFloat(textView_2.getText() + "");
mSubtraction = true;
textView_2.setText(null);
mEqualPressed = false;}
}
});

button_Mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (textView_2.getText()==""){
textView_2.setText("");
}
else if (textView_2.getText()=="-")
textView_2.setText(" ");
else{
textView_1.setText(textView_2.getText().toString()+"*");
mValue1 = Float.parseFloat(textView_2.getText() + "");
mMultiplication = true;
textView_2.setText(null);
mEqualPressed = false;}
}
});

button_Div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (textView_2.getText()==""){
textView_2.setText("");
}
else if (textView_2.getText()=="-")
textView_2.setText(" ");
else{
textView_1.setText(textView_2.getText().toString()+"/");
mValue1 = Float.parseFloat(textView_2.getText() + "");
mDivision = true;
textView_2.setText(null);
mEqualPressed = false;}
}
});

button_Sqrt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mEqualPressed = false;
mSqrt = true;
}
});

button_Expo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

if (textView_2.getText()==""){
textView_2.setText("");
}
else{
textView_1.setText(textView_2.getText()+"^");
mValue1 = Float.parseFloat(textView_2.getText() + "");
mExponential = true;
textView_2.setText(null);
mEqualPressed = false;}

}
});

button_Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textView_2.getText().length()<=1)
textView_2.setText(" ");
else
textView_2.setText((textView_2.getText()).subSequence(0, (textView_2.getText()).length()-1));


}
});

button_Clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

textView_2.setText(" ");
mEqualPressed = false;

}
});

button_Equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValue2 = Float.parseFloat(textView_2.getText() + "");

textView_1.setText(textView_1.getText() + textView_2.getText().toString());

if (mAddition){

textView_2.setText(mValue1 + mValue2 +"");
mAddition=false;
}


if (mSubtraction){
textView_2.setText(mValue1 - mValue2+"");
mSubtraction=false;
}

if (mMultiplication){
textView_2.setText(mValue1 * mValue2+"");
mMultiplication=false;
}

if (mDivision){
textView_2.setText(mValue1 / mValue2+"");
mDivision=false;
}

if (mSqrt){

textView_2.setText(Math.sqrt((double)mValue2)+"");
mSqrt = false;

}

if (mExponential ){

textView_2.setText(Math.pow((double) mValue1, (double)mValue2)+"");
mExponential = false;
}

mEqualPressed = true;
}
});


}
}

最佳答案

您添加了此内容

  textView_2 = (TextView) findViewById(R.id.textView2);

但忘记初始化textView_1的引用。

关于java - 为什么文本未显示在 textView_1 中且运算符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com