我有 2 个编辑文本字段,我从用户那里获取值,而不是我有四个按钮,当用户给出值并按下其中一个按钮时,程序将执行我分配给它的特定任务,它会给出结果在文本视图中,但我在添加两个值时出错。这是我的努力。
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView textView1;
Button button1,sub,mul,divi;
EditText editText1,editText2;
double a,b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
button1=(Button)findViewById(R.id.button1);
sub=(Button)findViewById(R.id.sub);
mul=(Button)findViewById(R.id.mul);
divi=(Button)findViewById(R.id.divi);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
a=Double.parseDouble(editText1.getText().toString());
b=Double.parseDouble(editText1.getText().toString());
if(arg0.getId()==R.id.button1){
textView1.setText(""+ a+b);
}
else{textView1.setText("not working ");}
}
}