重置EditText和TextView for android的代码

前端之家收集整理的这篇文章主要介绍了重置EditText和TextView for android的代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是编程新手.我做了一个简单的 Android应用程序,要求用户输入2个数字,做计算,点击计算按钮显示答案.现在我正在尝试设置重置按钮以清除所有字段.检查堆栈溢出的解决方案,但仍然无法弄清楚如何做到这一点.这是我的代码
public void calculate(View view)
 {

    EditText number1 = (EditText)findViewById(R.id.num1ID);
    EditText number2 = (EditText)findViewById(R.id.num2ID);

    Double num1Value = Double.parseDouble(number1.getText().toString());
    Double num2Value = Double.parseDouble(number2.getText().toString());

    Double resultValue = num1Value - num2Value;
    TextView resultDisplay = (TextView)findViewById(R.id.resultID);

    resultDisplay.setText(Double.toString(resultValue));
    }

谢谢.

解决方法

只需将按钮resetButton添加到布局
Button resetButton = (Button) findViewById(R.id.btnReset); 
    resetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            number1.setText("");
            number2.setText("");
            resultDisplay.setText("");
        }
    });
原文链接:https://www.f2er.com/android/313961.html

猜你在找的Android相关文章