电话计算时,小费计算器就开始了

时间:2017-02-02 07:52:37

标签: java android calculator

我的代码是:

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText amt = (EditText) findViewById(R.id.bill_amt);
    final EditText tip = (EditText) findViewById(R.id.bill_per);
    final TextView result = (TextView) findViewById(R.id.res);

    Button calc = (Button) findViewById(R.id.button1);
    calc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            double amount = Double.parseDouble(amt.toString());
            double tip_per = Double.parseDouble(tip.toString());
            double tip_cal = (amount * tip_per) / 100;
            result.setText("Result : " + Double.toString(tip_cal));
        }
    });
}

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

}   

请更正此编码中显示的错误 android studio也在Result.text上显示警告 关于使用占位符'

的资源字符串

i'm following this webpage's tutorial

1 个答案:

答案 0 :(得分:2)

您想将EditText解析为Double错误 - amt.toString()tip.toString()。哪个不正确(根本不可能)。

替换它:

double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());

有了这个:

double amount = Double.parseDouble(amt.getText().toString());
double tip_per = Double.parseDouble(tip.getText().toString());