尝试将TextField值乘以double值

时间:2015-08-07 22:53:32

标签: java android double

我试图获取inputEditText的值并将其乘以convertNum的值。

我得到的错误是:运营商'*'无法应用于'android.widget.EditText','double'

我相信我需要将inputEditText的值转换为数字,但我不确定。任何帮助,将不胜感激。这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    // Declare variables
    final Button calculateButton = (Button) findViewById(R.id.button);
    final TextView resultTextView = (TextView) findViewById(R.id.textView2);
    final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
    final EditText inputEditText = (EditText) findViewById(R.id.editText);
    final double convertNum = 0.125;

    // Set resultTextView to be invisible on app start
    resultTextView.setVisibility(View.INVISIBLE);

    // When calculate button is pressed: Set resultTextView to visible and show result
    calculateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            resultTextView.setVisibility(View.VISIBLE);
            resultnumTextView.setText(inputEditText * convertNum);

        }
    });
}

4 个答案:

答案 0 :(得分:1)

而不是:

resultnumTextView.setText(inputEditText * convertNum);

这样做:

resultnumTextView.setText(Double.parseDouble(inputEditText.getText().toString()) * convertNum);

答案 1 :(得分:1)

当您尝试将EditText(这是一个View实例)和double(这是一个数字)相乘时,会出现错误。

最简单的解决方法是替换

resultnumTextView.setText(inputEditText * convertNum);

resultnumTextView.setText(String.valueOf(
    Float.valueOf(inputEditText.getText().toString()) * convertNum
));

答案 2 :(得分:0)

inputEditText是一个EditText,一个用于写入文本的小部件。您需要提取键入的文本并转换为浮点数。

inputEditText.getText().toString() 

将为您提供带有键入文本的字符串。然后使用包装类Float转换为数字

 String s = inputEditText.getText().toString();
 Float f= Float.parseFloat(s);

正如您所见here

答案 3 :(得分:-2)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    // Declare variables
    final Button calculateButton = (Button) findViewById(R.id.button);
    final TextView resultTextView = (TextView) findViewById(R.id.textView2);
    final TextView resultnumTextView = (TextView) findViewById(R.id.textView3);
    final EditText inputEditText = (EditText) findViewById(R.id.editText);
    final double convertNum = 0.125;

    // Set resultTextView to be invisible on app start
    resultTextView.setVisibility(View.INVISIBLE);

    // When calculate button is pressed: Set resultTextView to visible and show result
    calculateButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String valueFromInput = inputEditText.getText().toString();
            try{
               double valueToDouble = Double.parseDouble(valueFromInput);
               double result = convertNum * valueToDouble;
               resultnumTextView.setText(result+"");
               resultTextView.setVisibility(View.VISIBLE);
            }catch(Exception e){
               // print the exception.
            }
        }
    });
}