将EditText输入转换为整数并相乘? [英] Convert EditText input to integer and multiply?

查看:125
本文介绍了将EditText输入转换为整数并相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Integer.parseInt(),但是它没有将EditText的String输入转换为整数.我需要使用整数来将数据相乘并计算总数. EditText不为空.为什么会发生此错误?

I tried using Integer.parseInt(), but it doesn't convert it EditText's String input to integer. I need to use integers to multiply data and calculate total. EditText is not empty. Why does this error occurs?

buttonAdd.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {
        try {
            if (twh.getText().toString() == "") {
                total = 0;
            } else {
                total = (Integer.parseInt(twh.getText().toString()));
            }
            a = Integer.parseInt(textIn1.getText().toString());
            b = Integer.parseInt(textIn2.getText().toString());
            c = Integer.parseInt(textIn3.getText().toString());
            total = total + (a * b * c);
            twh.setText(String.valueOf(total));
        }
        catch (Exception e){
            System.out.print(e+"");
        }

}

推荐答案

相反,

a = Integer.parseInt(textIn1.getText().toString());

尝试

a = Integer.valueOf(textIn1.getText().toString());

也添加

android:inputType="number"

在您的xml布局文件中.

in your xml layout file.

<EditText...
     android:inputType="number"/>

仅输入数字.

这篇关于将EditText输入转换为整数并相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆