我对这一切都很陌生,我正在尝试使用android studio为一个工作项目编写一个基本的应用程序。该应用程序的目的是计算出一个"答案= 1.25 x(待机x静止)+(1.75 x警报/ 2)的计算。 3个用户输入处于待机,静止和警报状态。 我已经构建了UI并完成了很多java但是我不能为我的生活找出当有人点击bt_calculate按钮时如何进行实际计算呢?
有人可以帮忙或至少指出我正确的方向吗?
package batterycalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BatteryCalculator extends AppCompatActivity {
private EditText QuiescentCurrent;
private EditText AlarmCurrent;
private EditText Standby;
private TextView ah_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery_calculator);
QuiescentCurrent = (EditText) findViewById(R.id.@+id/QuiescentCurrent);
AlarmCurrent = (EditText) findViewById(R.id.@+id/AlarmCurrent);
Standby = (EditText) findViewById(R.id.@+id/Standby);
Button bt_calculate = (Button) findViewById(R.id.bt_calculate);
bt_calculate = (TextView) findViewById(R.id.@+id/bt_calculate);
bt_calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeCalculations();
}
});
}
private void makeCalculations() {
///CALCULATION HERE
result.setText( result+"Ah");
}
}
编辑:现在已经进行了计算,但是对于我的生活,按下按钮时我无法使用它。正如我所说,我是新手,并试图自学java,但我看到这么多相互矛盾的方式使其工作但似乎没有一个对我有用!这是我目前的代码。有人可以帮忙吗?
package batterycalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BatteryCalculator extends AppCompatActivity {
private EditText quiescentcurrent;
private EditText alarmcurrent;
private EditText standbytime;
private TextView ahresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery_calculator);
quiescentcurrent = (EditText) findViewById(R.id.quiescentcurrent);
alarmcurrent = (EditText) findViewById(R.id.alarmcurrent);
standbytime = (EditText) findViewById(R.id.standbytime);
ahresult = (TextView) findViewById(R.id.ahresult);
Button btcalculate = (Button) findViewById(R.id.btcalculate);
btcalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeCalculations();
}
private void makeCalculations() {
double quiescent =
Double.parseDouble(quiescentcurrent.getText().toString());
double alarm =
Double.parseDouble(alarmcurrent.getText().toString());
double standby =
Double.parseDouble(standbytime.getText().toString());
double answer = 1.25 * (standby * quiescent) + (1.75 * alarm /
2);
ahresult.setText(String.valueOf(answer));
}
});
}
答案 0 :(得分:0)
private void makeCalculations() {
double quiescent = Double.parseDouble(QuiescentCurrent.getText().toString());
double alarm = Double.parseDouble(AlarmCurrent.getText().toString());
double standby= Double.parseDouble(Standby.getText().toString());
double answer = 1.25 * (standby * quiescent) + (1.75 * alarm/2);
result.setText(String.valueof(answer));
}
一些注意事项:
。您必须初始化result
TextView;
。您不应该使用大写字母启动变量名称;