所以,我是Android工作室的初学者,并尝试制作这个简单的应用程序,计算您总共饮用多少水。这个想法是,有人每周都会输入他们喝多少水。当我点击"计算它"计算一天的总数和平均值。
我的问题是每当我输入一个像小数点2,3的数字应用程序崩溃。我确实找到了一个解决方案,我需要进行验证(if,else)功能,但我不太确定如何在一个函数中选择所有日期。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBtn = (Button) findViewById(R.id.calculateBtn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editTextMon = (EditText) findViewById(R.id.editTextMon);
EditText editTextTue = (EditText) findViewById(R.id.editTextTue);
EditText editTextWed = (EditText) findViewById(R.id.editTextWed);
EditText editTextThu = (EditText) findViewById(R.id.editTextThu);
EditText editTextFri = (EditText) findViewById(R.id.editTextFri);
EditText editTextSat = (EditText) findViewById(R.id.editTextSat);
EditText editTextSun = (EditText) findViewById(R.id.editTextSun);
TextView resultTextView = (TextView) findViewById(R.id.resultTextView);
TextView errorView = (TextView) findViewById(R.id.errorView);
TextView averageTextView = (TextView) findViewById(R.id.averageTextView);
EditText allDays = (EditText) findViewById(R.id.editTextMon);
int mon = Integer.parseInt(editTextMon.getText().toString());
int tue = Integer.parseInt(editTextTue.getText().toString());
int wed = Integer.parseInt(editTextWed.getText().toString());
int thu = Integer.parseInt(editTextThu.getText().toString());
int fri = Integer.parseInt(editTextFri.getText().toString());
int sat = Integer.parseInt(editTextSat.getText().toString());
int sun = Integer.parseInt(editTextSun.getText().toString());
if ( ! (allDays.equals("") || allDays.equals("-")) ) {
int result = mon + tue + wed + thu + fri + sat + sun;
resultTextView.setText("Total: " + result + " L");
int average = result / 7;
averageTextView.setText("Avg: " + average + " L");
} else {
errorView.setText("Invalid Input");
}
}
});
}
}
答案 0 :(得分:2)
您正在以float
数据类型存储所有值。
使用Float.parseFloat(yourEditText.getText().toString());
存储它们并使用 content = "Example content"
filename = "example-file-name".
response = HttpResponse(content=content,
content_type='text/plain')
response['Content-Disposition'] = 'attachment; filename={0}'.format(filename)
return response
答案 1 :(得分:0)
在这种情况下,您可以使用Float x = Float.parseFloat(y);