今日は個人用アプリについて少し考えました。Androidを試したことはないので...助けていただければ幸いです。
私が欲しいのは2つの入力フィールドです。それらの値と*を相互に比較して、計算ボタンをタップした後に結果を出します。
私には問題ないように見えますが、私が言ったように、私はJavaにそれほど慣れておらず、Androidは初めてです。私の問題は、計算ボタンをタップすると、強制的に閉じる/動作を停止することです。前もって感謝します!
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<EditText android:id="@+id/fuel"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/fuel" />
<EditText android:id="@+id/numLaps"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/numLaps" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/calc"
android:text="@string/calc"
android:onClick="calc"/>
<EditText
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/output"
android:inputType="number"
/>
</LinearLayout>
MainActivity.java
package com.example.iracingfuelcalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements android.view.View.OnClickListener {
Button calc;
EditText numLaps,fuel, output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fuel = (EditText)findViewById(R.id.fuel);
numLaps = (EditText)findViewById(R.id.numLaps);
output = (EditText)findViewById(R.id.output);
calc = (Button)findViewById(R.id.calc);
calc.setOnClickListener(this);
}
public void onClick(View arg0){
int fuelValue = -1;
try {
fuelValue = Integer.parseInt(fuel.getText().toString());
}
catch (NumberFormatException e)
{
//you don't have to do much here since fuelValue already has a default value (-1)
}
int lapsValue = -1;
try {
lapsValue = Integer.parseInt(numLaps.getText().toString());
}
catch (NumberFormatException e)
{
}
int result = 0;
result = fuelValue * lapsValue;
result += Integer.parseInt(output.getText().toString());
output.setText("" + result);
}
}
文字列.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">iRacing Fuel Calc</string>
<string name="fuel">Fuel per lap</string>
<string name="numLaps">Number of laps</string>
<string name="calc">Calculate</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_display_message">TEST</string>
<string name="output">0</string>
</resources>