在android中添加两个数字

时间:2012-12-12 08:45:45

标签: android

我试图在android中创建一个计算器类型的添加。为此,我使用了以下代码。

但是ans总是0 ..

public class Main extends Activity {

    EditText num1,num2;
    TextView resu;
    int res;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button add=(Button) findViewById(R.id.add);
        num1=(EditText) findViewById(R.id.etnum1);
        num2=(EditText) findViewById(R.id.etnum2);
        resu=(TextView) findViewById(R.id.textView4);

        String mynum1=num1.getText().toString();
        final int mnum1=Integer.parseInt(mynum1);

        String mynum2=num2.getText().toString();
        final int mnum2=Integer.parseInt(mynum2);

        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                res=mnum1+mnum2;
                resu.setText(Integer.toString(res));
            }
        });
    }
}

我也试过

resu.setText(String.valueOf(res));

但答案总是为零!

我做错了什么?

7 个答案:

答案 0 :(得分:3)

包含用于在点击侦听器中获取数字的语句 如下所示:

public class Main extends Activity {

EditText num1,num2;
TextView resu;
int res;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button add=(Button) findViewById(R.id.add);
    num1=(EditText) findViewById(R.id.etnum1);
    num2=(EditText) findViewById(R.id.etnum2);
    resu=(TextView) findViewById(R.id.textView4);






    add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String mynum1=num1.getText().toString();
            String mynum2=num2.getText().toString();

            res=Integer.parseInt(mynum1)+Integer.parseInt(mynum2);
            resu.setText(Integer.toString(res));
        }
    });
}

}

答案 1 :(得分:0)

按下你的按钮setOnClickListener代码,并在解析为Integer之前检查edittext值是否为NULL或为空:

 add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
           int mnum1,mnum2;
         if(isNotNullNotEmptyNotWhiteSpaceOnlyByJava(num1.getText().toString())){

              mnum1=Integer.parseInt(num1.getText().toString());
         } 
         else{
                  mnum1=0;
          }  
         if(isNotNullNotEmptyNotWhiteSpaceOnlyByJava(num2.getText().toString())){

          mnum2=Integer.parseInt(num2.getText().toString());
         } 
         else{
               mnum2=0;
          }  

            res=mnum1+mnum2;
            resu.setText(Integer.toString(res));
        }
public  boolean isNotNullNotEmptyNotWhiteSpaceOnlyByJava(  
    String string)  
{  
   return string != null && !string.isEmpty() && !string.trim().isEmpty();  
} 

目前,您没有从EditText的

按钮上获得用户输入的最新值

答案 2 :(得分:0)

当活动创建时(<{1}}),您获得了用户输入。

单击按钮时需要输入用户输入,而不是在单击按钮时添加。将您的代码更改为:

onCreate

您应该使用try - catch,因为如果用户按下带有空白文本的按钮,此应用会崩溃。

答案 3 :(得分:0)

添加你可以使用:

     flot a=(flot)(((EditText)findViewById(R.id.et1)).getText().toString());
     flot b=(flot)(((EditText)findViewById(R.id.et2)).getText().toString());
     flot out=a+b

答案 4 :(得分:0)

public void onButtonClick(View v) {

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

    EditText e2 = (EditText) findViewById(R.id.editText2);
    TextView t1 = (TextView) findViewById(R.id.textView2);

    double num1=Double.parseDouble(e1.getText().toString());
    double num2=Double.parseDouble(e2.getText().toString());
    double sum= num1 + num2;
    t1.setText(Double.toString(sum));
}

答案 5 :(得分:0)

add.setOnClickListener(v -> 
{
    int mynum1 = Integer.parseInt(num1.getText().toString());

    int mynum2 = Integer.parseInt(num2.getText().toString());

    res = mynum1 + mynum2;
    resu.setText(String.toInteger(res));
});

答案 6 :(得分:-3)

完全披露:来自我的个人博客

Addition program in android

在android中添加两个数字的XML和Java代码

Mainactivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText editText,editText2,editText4;
    int v1,v2,v3;
    String s="ans";
private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
        editText=(EditText)findViewById(R.id.editText);
        editText2=(EditText)findViewById(R.id.editText2);
        editText4=(EditText)findViewById(R.id.editText4);
         v1=Integer.parseInt(editText.getText().toString());
         v2=Integer.parseInt(editText2.getText().toString());
         v3=v1+v2;

         s=String.valueOf(v3);
        editText4.setText(s);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    } 

activitymain.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <EditText
        android:layout_width="500dp"
        android:layout_height="40dp"
        android:id="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="60dp" />

    <EditText
        android:layout_width="500dp"
        android:layout_height="40dp"
        android:id="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/editText2" />
    <EditText
        android:layout_width="800dp"
        android:layout_height="40dp"
        android:id="@+id/editText4"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/editText" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>