简单的数学的EditText Android版 [英] Simple Edittext Math Android

查看:241
本文介绍了简单的数学的EditText Android版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新SO用户在这里,我是新来的Andr​​oid作为好。我需要帮助做两的EditText框之间一个简单的计算 - 没有一个按钮,利用textwatcher,以便用户使用的EditText车间完成后它可以在一个TextView添加两个一起并输出答案。我想在这里发表我的code,但它只是没有任何好处。任何人都可以提供如何采取两种的EditText箱,textwatcher分配给这一个例子,然后将输出到一个TextView。另外,请记住,这是最终将同时autoupdating对方使用多个的EditText框。这将是类似于Microsoft Excel中创建forumulas,并让他们立即更新。其中一个我似乎有被捕捉NumberFormatException的时候只是一个空的主要问题。感谢您的时间。

new SO user here and I'm new to android as well. I need help doing a simple calculation between two edittext boxes - without a button, using a textwatcher, so that after the user is done using the edittext boxes it can add the two together and output the answer in a textview. I'd post my code here, but it just isn't any good. Can anyone provide an example of how to take two edittext boxes, assign a textwatcher to both, then put the output into a textview. Also, please keep in mind that this is eventually going to use multiple edittext boxes while autoupdating each other. This would be similar to creating forumulas in Microsoft excel and having them update immediately. One of the main problems I seem to have is catching the numberformatexception when just one is empty. Thank you for your time.

推荐答案

我实现了一个简单的应用程序 - 我希望它可以帮助你。

I implemented a simple app - I hope it helps you.

public class MainActivity extends Activity implements TextWatcher {

TextView tvResult;
EditText tvNumberOne, tvNumberTwo;

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

    tvResult = (TextView)findViewById(R.id.tvResult);
    tvNumberOne = (EditText)findViewById(R.id.tvNumberOne);
    tvNumberTwo = (EditText)findViewById(R.id.tvNumberTwo);

    tvNumberOne.addTextChangedListener(this);
    tvNumberTwo.addTextChangedListener(this);

}

@Override
public void afterTextChanged(Editable s) {

    int numOne = 0, numTwo = 0;
    try{
        numOne = Integer.valueOf(String.valueOf(tvNumberOne.getText()));
        numTwo = Integer.valueOf(String.valueOf(tvNumberTwo.getText()));
    }catch(Exception ex){
        Toast.makeText(getApplicationContext(), "Parsing error!",Toast.LENGTH_SHORT).show();
    }

    tvResult.setText(String.valueOf(numOne + numTwo));

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

}

这篇关于简单的数学的EditText Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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