将代码中的英文文本替换成中文文本,例如Clear”替换成“清除”,Calculator”替换成“计算器”等。
代码示例:
public class MainActivity extends AppCompatActivity {
private EditText mResultEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultEt = findViewById(R.id.result_et);
}
public void onNumberClick(View view) {
Button button = (Button) view;
mResultEt.append(button.getText().toString());
}
public void onClearClick(View view) {
mResultEt.setText("");
}
public void onEqualsClick(View view) {
String expression = mResultEt.getText().toString();
try {
Expression e = new ExpressionBuilder(expression).build();
double result = e.evaluate();
mResultEt.setText(Double.toString(result));
} catch (Exception ex) {
Toast.makeText(this, "输入表达式有误!", Toast.LENGTH_SHORT).show();
}
}
}
XML布局文件示例: