提问人:AndraKenshin 提问时间:1/9/2023 最后编辑:greg-449AndraKenshin 更新时间:1/9/2023 访问量:44
如何将Textview输出用于另一个类方法?
How to use Textview output for another class method?
问:
我刚刚开始使用 eclipse adt(android 开发者工具),我正在开发 electricbill 计算器应用程序。应用界面如下所示: 应用界面
用户将输入之前的消耗量和当前消耗量的值,然后计算(第一个按钮)以获得总消耗量。然后输入费率并计算(第二个按钮,乘以总消耗量和费率)以获得电费单。
我的主要活动包含按钮的 onClickListeners。计算的第一部分有效,但是当我尝试输入一个值并对其进行速率和计算时,应用程序崩溃了,我不知道问题出在哪里。这是我的代码:
“计算”按钮
public class MainActivity extends Activity {
EditText PrevConEdt, CurrConEdt, RateEdt;
TextView ConsumptionTv, ElectricBillTv;
Button CalculateBtn, ComputeBtn, ClearBtn, ClearBtn2;
Double rateDbl, cbillDbl, consumptionDbl, prevDbl, currentDbl;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PrevConEdt = (EditText) findViewById(R.id.txtPreviousConsumption);
CurrConEdt = (EditText) findViewById(R.id.txtCurrentConsumption);
RateEdt = (EditText) findViewById(R.id.txtEnterRate);
ConsumptionTv = (TextView) findViewById(R.id.txtConsumption);
ElectricBillTv = (TextView) findViewById(R.id.txtEbill);
CalculateBtn = (Button) findViewById(R.id.btnCalculate);
CalculateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View Calculate) {
String prev, current, consumption;
prev = PrevConEdt.getText().toString();
current = CurrConEdt.getText().toString();
if(PrevConEdt.length() == 0){
Toast.makeText(MainActivity.this, "Consumption Required", Toast.LENGTH_LONG).show();
return;
}else if(CurrConEdt.length() == 0){
Toast.makeText(MainActivity.this, "Consumption Required", Toast.LENGTH_LONG).show();
return;
}else{
Double prevDbl, currentDbl, consumptionDbl;
prevDbl = Double.parseDouble(prev);
currentDbl = Double.parseDouble(current);
if(currentDbl < prevDbl){
//String msg = "Current should be greater than previous.";
//int duration = Toast.LENGTH_LONG;
//Toast.makeText(getBaseContext(), msg, duration).show();
//PrevConEdt.requestFocus();
CurrConEdt.setError("Current should be greater than previous.");
return;
}else{
consumptionDbl = currentDbl - prevDbl;
String consumptionStr = String.format("%.2f kWh", consumptionDbl);
ConsumptionTv.setText(consumptionStr);
}
}
}
});
计算按钮
ComputeBtn = (Button) findViewById(R.id.btnCompute);
ComputeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View compute) {
String rate, consumption;
rate = RateEdt.getText().toString();
consumption = ConsumptionTv.getText().toString();
if(RateEdt.length() == 0){
RateEdt.setError("Rate is required.");
return;
}else{
Double rateDbl, cbillDbl, consumptionDbl;
rateDbl = Double.parseDouble(rate);
consumptionDbl = Double.parseDouble(consumption);
cbillDbl = consumptionDbl * rateDbl;
String billStr = String.format("Php %.2f", cbillDbl);
ElectricBillTv.setText(billStr);
}
}
});
答: 暂无答案
评论