-2

我试图在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));

但答案总是零!

我做错了什么?

4

7 回答 7

4

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

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));
        }
    });
}

}

于 2012-12-12T09:08:54.647 回答
0

您在创建活动时获得了用户输入(在 中onCreate)。

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

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); 

        add.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

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

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

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

于 2013-12-08T04:39:20.710 回答
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 的按钮单击获得用户输入的最新值

于 2012-12-12T08:47:26.053 回答
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));
});
于 2021-07-27T00:51:54.123 回答
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
于 2016-07-08T10:40:43.390 回答
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));
}
于 2017-10-14T05:01:24.083 回答
-3

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

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);
    } 

活动主.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>
于 2016-02-20T02:57:00.797 回答