我在android中制作计算器,但出于某种原因它不会让我从编辑文本中读取。这是我用来声明编辑文本的代码:
final EditText AnswerBox = (EditText) findViewById(R.id.AnswerBox);
这是其中一个按钮的代码:
one.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
AnswerBox.append("1");
}
});
}
这是读取数字的方法:
public Double number_reader()
{
Double num1;
String s;
s=AnswerBox.getText().toString();
num1=Double.valueOf(s);
return num1;
}
问题在于number_reader方法中的AnswerBox,它说它无法解析..
答案 0 :(得分:1)
您可能在AnswerBox
方法中声明onCreate
:
final EditText AnswerBox = (EditText) findViewById(R.id.AnswerBox);
现在AnswerBox
是一个局部变量,它不存在于onCreate
方法之外,因此无法在方法number_reader
中找到它。
要解决此问题,请在班级中创建私人字段:
private final EditText AnserBox;
然后在onCreate
方法中执行此操作:
AnswerBox = (EditText) findViewById(R.id.AnswerBox);
答案 1 :(得分:0)
你宣布它是最终的并将其创建为空。怀疑你可以在那之后添加它。
答案 2 :(得分:0)
我认为您需要使用Double.parseDouble(s)。如果您需要,可以参考我给出的答案here
答案 3 :(得分:0)
删除final关键字并在类字段中添加EditText(在所有方法之外声明,但在Activity中)。之后,可以读取和写入EditText。 另外,对于附注,使用大写首字母的变量名称不是一个好习惯,这适用于类或接口。尝试使用较低的camelCase。
答案 4 :(得分:0)
你可以尝试这段代码..
package com.example.showoff;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.DigitsKeyListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView edittest1;
Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;
ArrayList<Float> math = new ArrayList<Float>();
float m1;
float m2;
float temp;
int currentOperation = 0;
int nextOperation;
final static int ADD = 1;
final static int SUBTRACT = 2;
final static int MULTIPLY =3;
final static int DIVISION = 4;
final static int EQUALS = 0;
final static int CLEAR = 1;
final static int DONT_CLEAR = 0;
int clearDisplay = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittest1 = (TextView) findViewById(R.id.edittest1);
one =(Button) findViewById(R.id.btnNum1Id);
two =(Button) findViewById(R.id.btnNum2Id);
three =(Button) findViewById(R.id.btnNum3Id);
four =(Button) findViewById(R.id.btnNum4Id);
five =(Button) findViewById(R.id.btnNum5Id);
six =(Button) findViewById(R.id.btnNum6Id);
seven =(Button) findViewById(R.id.btnNum7Id);
eight =(Button) findViewById(R.id.btnNum8Id);
nine =(Button) findViewById(R.id.btnNum9Id);
zero =(Button) findViewById(R.id.btnNum0Id);
add =(Button) findViewById(R.id.btnNumAddId);
sub =(Button) findViewById(R.id.btnNumSubId);
mul =(Button) findViewById(R.id.btnNumMulId);
div =(Button) findViewById(R.id.btnNumDivId);
cancel =(Button) findViewById(R.id.btnNumClearId);
equal =(Button) findViewById(R.id.btnNumEqualId);
edittest1.setKeyListener(DigitsKeyListener.getInstance(true,true));
registerListeners();
}
private void registerListeners() {
// TODO Auto-generated method stub
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("1");
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("2");
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("3");
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("4");
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("5");
}
});
six.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("6");
}
});
seven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("7");
}
});
eight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("8");
}
});
nine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("9");
}
});
zero.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clearDisplay == CLEAR) {
edittest1.setText("");
}
clearDisplay = DONT_CLEAR;
edittest1.append("0");
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(ADD);
}
}
);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(SUBTRACT);
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(MULTIPLY);
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(DIVISION);
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calcLogic(EQUALS);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edittest1.setText("0");
m1 = 0;
m2 = 0;
math.removeAll(math);
currentOperation = 0;
nextOperation = 0;
}
});
}
private void calcLogic(int operator){
math.add(Float.parseFloat(edittest1.getText().toString()));
if (operator != EQUALS) {
nextOperation = operator;
}
else if (operator == EQUALS){
nextOperation = 0;
//operator=' ';
}
switch (currentOperation) {
case ADD:
m1 = math.get(0);
m2 = math.get(1);
math.removeAll(math);
math.add(m1 + m2);
edittest1.setText(String.format("%.3f", math.get(0)));
break;
case SUBTRACT:
m1 = math.get(0);
m2 = math.get(1);
math.removeAll(math);
math.add(m1 - m2);
edittest1.setText(String.format("%.3f", math.get(0)));
break;
case MULTIPLY:
m1 = math.get(0);
m2 = math.get(1);
math.removeAll(math);
math.add(m1 * m2);
edittest1.setText(String.format("%.3f", math.get(0)));
break;
case DIVISION:
m1 = math.get(0);
m2 = math.get(1);
math.removeAll(math);
math.add(m1 / m2);
edittest1.setText(String.format("%.3f", math.get(0)));
break;
}
clearDisplay = CLEAR;
currentOperation = nextOperation;
if (operator == EQUALS) {
m1 = 0;
m2 = 0;
math.removeAll(math);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}