📅  最后修改于: 2023-12-03 15:14:50.807000             🧑  作者: Mango
在Android开发中,我们经常需要使用到计算器来进行一些简单的运算,为此,Android提供了一个内置的计算器来满足我们的需求。这个内置的计算器可以将EditText控件转化为一个功能完整的计算器,方便我们进行计算。下面,我们来一起探讨一下如何使用这个内置的计算器。
实现EditText内置计算器的步骤如下:
在布局文件中添加一个EditText控件,设置其输入类型为数字:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="请输入需要计算的表达式"/>
在Activity中获取EditText控件的实例,并设置其onFocusChangeListener:
EditText editText = findViewById(R.id.editText);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b) {
new Calculator().show(getSupportFragmentManager(), "CalculatorDialog");
}
}
});
创建一个Calculator类,继承自DialogFragment,并重写其onCreateDialog方法:
public class Calculator extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final EditText editText = getActivity().findViewById(R.id.editText);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("计算器");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.calculator_layout, null);
builder.setView(view);
Button btn0 = view.findViewById(R.id.btn0);
Button btn1 = view.findViewById(R.id.btn1);
Button btn2 = view.findViewById(R.id.btn2);
Button btn3 = view.findViewById(R.id.btn3);
Button btn4 = view.findViewById(R.id.btn4);
Button btn5 = view.findViewById(R.id.btn5);
Button btn6 = view.findViewById(R.id.btn6);
Button btn7 = view.findViewById(R.id.btn7);
Button btn8 = view.findViewById(R.id.btn8);
Button btn9 = view.findViewById(R.id.btn9);
Button btnAdd = view.findViewById(R.id.btnAdd);
Button btnSub = view.findViewById(R.id.btnSub);
Button btnMul = view.findViewById(R.id.btnMul);
Button btnDiv = view.findViewById(R.id.btnDiv);
Button btnDot = view.findViewById(R.id.btnDot);
Button btnDel = view.findViewById(R.id.btnDel);
Button btnEqual = view.findViewById(R.id.btnEqual);
Button btnCancel = view.findViewById(R.id.btnCancel);
final StringBuilder stringBuilder = new StringBuilder();
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn0:
stringBuilder.append("0");
break;
case R.id.btn1:
stringBuilder.append("1");
break;
case R.id.btn2:
stringBuilder.append("2");
break;
case R.id.btn3:
stringBuilder.append("3");
break;
case R.id.btn4:
stringBuilder.append("4");
break;
case R.id.btn5:
stringBuilder.append("5");
break;
case R.id.btn6:
stringBuilder.append("6");
break;
case R.id.btn7:
stringBuilder.append("7");
break;
case R.id.btn8:
stringBuilder.append("8");
break;
case R.id.btn9:
stringBuilder.append("9");
break;
case R.id.btnAdd:
stringBuilder.append("+");
break;
case R.id.btnSub:
stringBuilder.append("-");
break;
case R.id.btnMul:
stringBuilder.append("*");
break;
case R.id.btnDiv:
stringBuilder.append("/");
break;
case R.id.btnDot:
stringBuilder.append(".");
break;
case R.id.btnDel:
if (stringBuilder.length() > 0) {
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
break;
case R.id.btnEqual:
String str = stringBuilder.toString();
if (stringBuilder.length() > 0 && str.indexOf("/") == -1) {
editText.setText(String.valueOf(CalculatorUtils.cal(str)));
} else {
editText.setText("");
}
break;
case R.id.btnCancel:
Calculator.this.dismiss();
break;
}
editText.setText(stringBuilder.toString());
}
};
btn0.setOnClickListener(listener);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
btn4.setOnClickListener(listener);
btn5.setOnClickListener(listener);
btn6.setOnClickListener(listener);
btn7.setOnClickListener(listener);
btn8.setOnClickListener(listener);
btn9.setOnClickListener(listener);
btnAdd.setOnClickListener(listener);
btnSub.setOnClickListener(listener);
btnMul.setOnClickListener(listener);
btnDiv.setOnClickListener(listener);
btnDot.setOnClickListener(listener);
btnDel.setOnClickListener(listener);
btnEqual.setOnClickListener(listener);
btnCancel.setOnClickListener(listener);
return builder.create();
}
}
创建一个CalculatorUtils工具类,用于计算表达式:
public class CalculatorUtils {
public static double cal(String str) {
double ret = 0.0;
try {
Expression expression = new ExpressionBuilder(str).build();
ret = expression.evaluate();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
在AndroidManifest.xml文件中添加以下权限:
<uses-permission android:name="android.permission.INTERNET"/>
至此,我们便成功地实现了EditText的内置计算器功能。
完整的代码片段请参考 GitHub。