今天对个人应用程序有点小小的想法。从来没有尝试过Android,所以...任何帮助表示赞赏。使用Android进行计算
我想要的是2个输入字段,取这些值& *它们相互对接,在点击calc按钮后给出结果。
对我来说看起来还不错,但就像我说过的,我对Java并不熟悉,对Android也不熟悉。我的问题是,当我点击Calc按钮强制关闭/停止工作。提前致谢!
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);
}
}
的strings.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>
你在强制关闭时提供了堆栈跟踪吗?如果是的话,张贴它。 – mango
你应该确保你的应用获得专利,然后再告诉世界它只是说。 – 2012-12-26 23:20:00
Abe,其他应用程序都在那里,完全一样,我想要的。作为一名学生,在假期后学习更多关于Java的知识,我想提高自己的技能。谢谢,不过。 –