好吧,我得到了这个代码,它与所有正常数字完美配合但是当我运行它并给出第一个或第二个EditText(0.1,0.2,0.3,0.4)或任何其他数字以(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);
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));
}
});
}
}
答案 0 :(得分:0)
您需要将EditTextvalues转换为Float,如:
String mynum1=num1.getText().toString();
float mnum1= Float.parseFloat(mynum1);
String mynum2=num2.getText().toString();
float mnum2= Float.parseFloat(mynum2);
float res=mnum1*mnum2;
resu.setText(String.valueOf(res));
希望这可以帮到你!
答案 1 :(得分:0)
EditText num1,num2;
TextView resu;
double res;
在你的onClickview()中
double mnum1=Double.parseDouble(mynum1);
String mynum2=num2.getText().toString();
double mnum2=Double.parseDouble(mynum2);
res=mnum1*mnum2;
resu.setText(Double.toString(res));