用户输入的简单数学

时间:2013-06-10 06:41:29

标签: android math

我是java和Android的新手。我正在研究几乎完成的简单项目。但我在XML布局中面临一些问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/a"
        android:layout_width="35dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:inputType="number"
        android:minWidth="60dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView 
        android:id="@+id/total"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp" />

</LinearLayout>

当用户在编辑文本中输入数字并按下按钮时,文本视图应显示以下结果:

"enters number minus(-)30"  

如果用户输入数字50,则结果显示在文本框中显示20(50-30 = 20)

我只知道基本的Java,我之前从未用Java做数学,所以我不知道我在代码中写了什么。在谷歌和stackoverflow.com上搜索了很多,也阅读了很多书,但从未找到那么简单的数学。

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

btnsub.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String edtval = edttxt.getText().toString().trim();

                           if(!edtval.equals("")){
                               int val = Integer.parseInt(edtval);
                               int finalval = val - 30;

                              textview.setText(finalval+"");
                         }
            }
        });

答案 1 :(得分:1)

btnsub.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            String edtval = edttxt.getText().toString().trim();

                       if(!edtval.equals("")){
                           int val = Integer.parseInt(edtval);
                           int finalval = val - 30;

                          textview.setText(String.valueOf(finalval));
                     }
        }
    });

答案 2 :(得分:0)

简单的数学运算非常简单。

您首先需要获得对视图的引用,如下所示

EditText editText = (EditText) findViewById(R.id.a);

Button button = (Button) findViewById(R.id.button1);

TextView textView = (TextView) findViewById(R.id.total);

然后为Button设置onClickListener并在onClick方法

中执行操作
button.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v) {
          Integer input = 0;
          try {
              input = Integer.parseInt(editText.getText().toString());
          } catch (NumberFormatException e) {
              input = 0;
          } 
          input = input - 30;
          textView.setText(input.toString() + "");
     }
}

答案 3 :(得分:-1)

btnsub.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View v) {
  int num;
        try {
              num= Integer.parseInt(edttxt.getText().toString());
              textview.setText(""+(num - 30));
         } catch (NumberFormatException e) {
             // Open a dialog for incorrect input,i.e., Not a number
         }

        }
    });
相关问题