0
我正在嘗試製作Android應用,而我現在正在處理的功能是計算用戶輸入的數字的平方根。如何使用用戶在輸入框中輸入的數字的平方根而不是數字本身?
如何獲取用戶在文本框中輸入的數字,並在我的程序的doCalc
部分中使用該數字的平方根?我將數字限制爲1到20之間的整數。例如,如果用戶在輸入框中輸入2
,我想在doCalc
方法中使用1.41
。
這裏是我的.java代碼:
package learn.text;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LearntextActivity extends Activity {
TextView text;
EditText input;
TextView text2;
EditText input2;
TextView text3;
EditText input3;
Button calc;
TextView output;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
text.setText("Enter the design GPM for Chiller");
input = (EditText) findViewById(R.id.input);
text2 = (TextView) findViewById(R.id.text2);
text2.setText("Enter the Square root of the actual pressure drop across the coil");
input2 = (EditText) findViewById(R.id.input2);
text3 = (TextView) findViewById(R.id.text3);
text3.setText("Enter the design pressure drop of coil");
input3 = (EditText) findViewById(R.id.input3);
calc = (Button) findViewById(R.id.calc);
output = (TextView) findViewById(R.id.output);
}
public void doCalc (View view) {
double mInput = Double.parseDouble(input.getText().toString());
double mInput2 = Double.parseDouble(input2.getText().toString());
double mInput3 = Double.parseDouble(input3.getText().toString());
double mOutput = (mInput*mInput2)/(mInput3);
output.setText("GPM is" + mOutput);
}
}
這裏是.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input"></EditText>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text2"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input2"></EditText>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/text3"></TextView>
<EditText android:layout_height="wrap_content" android:text="" android:layout_width="match_parent" android:id="@+id/input3"></EditText>
<Button android:layout_height="wrap_content" android:text="Get GPM" android:layout_width="wrap_content" android:id="@+id/calc" android:password="false" android:onClick="doCalc"></Button>
<TextView android:layout_height="wrap_content" android:text="" android:layout_width="wrap_content" android:id="@+id/output"></TextView>
</LinearLayout>
這裏是清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="learn.text"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher" android:label="string/app_name">
android:label="@string/app_name" >
<activity
android:name=".LearntextActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ruakh您沒有錯過任何東西。謝謝你的答案。我把它放在代碼中,它很好用!我對編程非常陌生。非常感謝你。 – Droidxuser1 2012-02-01 04:29:11
@ Droidxuser1:不客氣!我不得不說,創建一個Android應用程序看起來像是一個非常雄心勃勃的第一個編程項目 - 即使通過一個教程,就像你看起來那樣。祝你好運! :-) – ruakh 2012-02-01 04:38:13
我喜歡學習新事物的挑戰,我需要在手機上使用這個應用程序才能工作。 – Droidxuser1 2012-02-01 04:46:42