2012-02-01 54 views
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> 

回答

3

也许我误会东西,但—你不能只是改变

Double.parseDouble(input2.getText().toString()) 

Math.sqrt(Double.parseDouble(input2.getText().toString())) 

? (有关Math.sqrt的文档,请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double)。)

+0

ruakh您没有错过任何东西。谢谢你的答案。我把它放在代码中,它很好用!我对编程非常陌生。非常感谢你。 – Droidxuser1 2012-02-01 04:29:11

+0

@ Droidxuser1:不客气!我不得不说,创建一个Android应用程序看起来像是一个非常雄心勃勃的第一个编程项目 - 即使通过一个教程,就像你看起来那样。祝你好运! :-) – ruakh 2012-02-01 04:38:13

+0

我喜欢学习新事物的挑战,我需要在手机上使用这个应用程序才能工作。 – Droidxuser1 2012-02-01 04:46:42

相关问题