Android的 - 如何把一个EditText(数字),然后将其转换为整数,使用它的数学? [英] Android - How to take an EditText (numbers) then convert it to an integer to use it for math?

查看:1434
本文介绍了Android的 - 如何把一个EditText(数字),然后将其转换为整数,使用它的数学?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何采取一个EditText区域,用户可以输入一个号码中,然后使之成为整数,因此它可以被用在程序的加,减,划分等Basicaly我需要测试的输入能在一个计算器,这将是中的code和然后它需要被放入一个TextView或字符串,所以最终的答案可以被用户看到被使用。

更多信息: 我有三个的EditText领域,用户必须填写所有三个那么preSS等于,它会告诉他们答案,所有的数学和这种需求是在code,因此用户只需输入信息而实际上并没有计算任何东西。 感谢您的帮助,请咨询我,如果你需要任何更多的信息。

最好的办法做到这一点,在code将帮助我很多东西。

我已经加入了code的建议,但我仍然得到一个错误的parseInt函数,我仍然需要添加一个按钮来计算的EditText地区输入的数字。我将如何做到这一点?对不起,我是相当Java和Android的发展。

 包com.example.one;


             进口com.example.one.R;

             进口android.app.Activity;
             进口android.content.Intent;
             进口android.os.Bundle;
             进口android.text.Editable;
             进口android.view.View;
             进口android.widget.Button;
             进口android.widget.EditText;

             公共类monthlyp延伸活动{

         @覆盖
        保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.monthlyp);


   EditText上editone =(EditText上)findViewById(R.id.editone);
   EditText上editoneValue =(EditText上)editone.getText();
   INT editoneNum =的Integer.parseInt(editoneValue);

    }
   ;

   }
 

解决方案

首先,获得的EditText的实例:

 的EditText为myedit =(EditText上)findViewById(R.id.edittext1);
 

然后得到目前正在显示的字符串:

 字符串myEditValue = myEdit.getText()的toString()。
 

然后解析为一个整数:

  INT myEditNum =的Integer.parseInt(myEditValue);
 

I am wondering how to take an EditText area that the user can enter a number in, then make it an integer so it can be used in the program for adding, subtracting, dividing, etc. Basicaly I need the test the enter to be able to be used in a calculator which will be within the code and then it needs to be put into a TextView or string so the final answer can be seen by the user.

More info: I have three EditText areas and the user has to fill in all three then press "equals" and it will tell them the answer, all the math and such needs to be in the code so the user just enters information and doesn't actually calculate anything. Thanks for the help, please ask me if you need any more info.

The best way to do this and the code would help me a lot.

I have added the code that was suggested but I still get an error on parseInt and I still need to add a button to calculate the numbers entered in the EditText areas. How would I do this? Sorry I am fairly to Java and Android development.

             package com.example.one;


             import com.example.one.R;

             import android.app.Activity;
             import android.content.Intent;
             import android.os.Bundle;
             import android.text.Editable;
             import android.view.View;
             import android.widget.Button;
             import android.widget.EditText;

             public class monthlyp extends Activity{

         @Override
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monthlyp);


   EditText editone = (EditText) findViewById(R.id.editone);
   EditText editoneValue = (EditText) editone.getText();
   int editoneNum = Integer.parseInt(editoneValue);

    }
   ;

   }

解决方案

First, get an instance of the EditText:

EditText myEdit = (EditText) findViewById(R.id.edittext1);

Then get the string that is currently being displayed:

String myEditValue = myEdit.getText().toString();

Then parse it for an integer:

int myEditNum = Integer.parseInt(myEditValue);

这篇关于Android的 - 如何把一个EditText(数字),然后将其转换为整数,使用它的数学?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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