燃料消費量を監視するためのAndroidアプリを作成しようとしています。私は4つのEditTextを持っています。
- キロメートル読み
- 燃料量
- 燃料価格
- 総費用
Km 読み取り値の入力は必須ですが、残りの 3 つのうち任意の 2 つをユーザーが入力する必要があります。したがって、ユーザーが 3 番目の Edittext をクリックすると、他の 2 つの助けを借りて取得された計算値が自動的に取得されます。
やあ!
すべての提案をありがとう..いいえ、私はあなたたち全員が私のために働くことを望んでいませんでした. ここで初めて質問してみました。自分で考えて、コードをここに示したので、将来それが必要な人にとって役立つかもしれません。
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fuelexpense);
ETKm = (EditText)findViewById(R.id.ETKm);
ETFuelQty = (EditText)findViewById(R.id.ETFuelQty);
ETFuelPrice = (EditText)findViewById(R.id.ETFuelPrice);
ETTotalCost = (EditText)findViewById(R.id.ETTotalCost);
btnSave = (Button)findViewById(R.id.btnSave);
btnClear = (Button)findViewById(R.id.btnClear);
chkFullTank = (CheckBox)findViewById(R.id.chkFullTank);
btnSave.setOnClickListener(this);
btnClear.setOnClickListener(this);
ETFuelQty.setOnFocusChangeListener(this);
ETFuelPrice.setOnFocusChangeListener(this);
ETTotalCost.setOnFocusChangeListener(this);
}
public void onFocusChange(View EditTextFocus , boolean hasFocus)
{
// TODO Auto-generated method stub
try
{
km= Long.parseLong(ETKm.getText().toString());
fuelQty= Integer.parseInt(ETFuelQty.getText().toString());
fuelPrice= Double.parseDouble(ETFuelPrice.getText().toString());
totalCost= Double.parseDouble(ETTotalCost.getText().toString());
}
catch(NumberFormatException ne)
{
ne.printStackTrace();
}
if(ETTotalCost.hasFocus())
{
if((fuelQty!=0)&&(fuelPrice!=0))
totalCost=fuelQty*fuelPrice;
ETTotalCost.setText(newDecimalFormat("##.##").format(totalCost));
}
else if(ETFuelQty.hasFocus())
{
if((fuelPrice!=0)&&(totalCost!=0))
fuelQty= (int) (totalCost/fuelPrice);
ETFuelQty.setText(String.valueOf(fuelQty));
}
else if(ETFuelPrice.hasFocus())
{
if((fuelQty!=0)&&(totalCost!=0))
fuelPrice=totalCost/fuelQty;
ETFuelPrice.setText(String.valueOf(fuelPrice));
}
}
私のために働いた..ありがとう