gpt4 book ai didi

java - 计算器中的等于按钮什么都不做(Android 开发者工具)

转载 作者:行者123 更新时间:2023-11-30 01:59:01 24 4
gpt4 key购买 nike

我真的想不通,我是编程初学者,做了大约 2 个月。我为 Android 制作了计算器,但问题是等于按钮什么都不做。

设置不同的变量:

Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;

除了等号之外的所有按钮都只是将各自的文本附加到变量结果。

这里是只有等号按钮的代码(创建只是为了测试,但失败了):

    equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

String expression = String.valueOf(result);

if(expression.contains("+")){
String[] numbers = expression.split("\\+");
String no1 = numbers[0];
String no2 = numbers[1];

Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;

theresult = String.valueOf(added);
result.setText(theresult);


}
}
});

我在这里使用了 if ,这样我就可以使用 else if 来使其他操作起作用。稍后我也会使用 Try-Catch block 。感谢您的宝贵时间!

MainActivity.java 完整代码

package org.example.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {

Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,multiply,dot,equals;
ImageButton clear;
TextView result;
String theresult;




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

result = (TextView) findViewById(R.id.result);

one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("1");
}
});


two = (Button) findViewById(R.id.two);
two.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("2");
}
});

three = (Button) findViewById(R.id.three);
three.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("3");
}
});

four = (Button) findViewById(R.id.four);
four.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("4");
}
});

five = (Button) findViewById(R.id.five);
five.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("5");
}
});


six = (Button) findViewById(R.id.six);
six.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("6");
}
});


seven = (Button) findViewById(R.id.seven);
seven.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("7");
}
});

eight = (Button) findViewById(R.id.eight);
eight.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("8");
}
});

nine = (Button) findViewById(R.id.nine);
nine.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("9");
}
});


zero = (Button) findViewById(R.id.zero);
zero.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("0");
}
});

dot = (Button) findViewById(R.id.dot);
dot.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append(".");
}
});


clear = (ImageButton) findViewById(R.id.clear);
clear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.setText("");
}
});


plus = (Button) findViewById(R.id.plus);
plus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("+");
}
});

minus = (Button) findViewById(R.id.minus);
minus.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("-");
}
});

divide = (Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("/");
}
});


multiply = (Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

result.append("x");
}
});


equals = (Button) findViewById(R.id.equals);
equals.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
result.getText();
String expression = String.valueOf(result);

if(expression.contains("+")){
String[] numbers = expression.split("\\+");
String no1 = numbers[0];
String no2 = numbers[1];

Double numb1 = Double.valueOf(no1);
Double numb2 = Double.valueOf(no2);
Double added = numb1+numb2;

theresult = String.valueOf(added);
result.setText(theresult);


}
}
});




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

最佳答案

就这样做:

@Override
public void onClick(View v) {
String expression = result.getText().toString();
// Log.d("LOG", expression);

if(expression.contains("+")){
(...)

也许工作。无需使用 String.valueOf()

关于java - 计算器中的等于按钮什么都不做(Android 开发者工具),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31772340/

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