¿Cómo puedo usar la raíz cuadrada de un número que el usuario ingresa en un cuadro de entrada, en lugar del número en sí?

I'm trying to make an Android app, and the feature that I'm working on right now is calculating the square root of a number entered by the user.

How can I take a number that the user enters in a text box, and use the square root of that number in the doCalc part of my program? I'm limiting the number to be an integer between 1 and 20. For example, if the user enters 2 in the input box, I want to use 1.41 en el objeto doCalc método.

Here is my .java code:

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. */
    public void onCreate(Bundle savedInstanceState) {
        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);

Here is the .xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >

        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>

Aquí está el archivo de manifiesto:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

        android:icon="@drawable/ic_launcher" android:label="string/app_name"> 
        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

preguntado el 01 de febrero de 12 a las 03:02

1 Respuestas

Probably I'm misunderstanding something, but — can't you just change




? (Ver http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double) for documentation of Math.sqrt.)

Respondido 01 Feb 12, 08:02

ruakh you did not miss anything. Thank you for the answer. I put it in the code and it works great! I am very new to programming. Thank you very much. - Droidxuser1

@Droidxuser1: You're welcome! I have to say, creating an Android app seems like a very ambitious first programming project -- even going through a tutorial, as you seem to be. I wish you the best of luck with it! :-) - ruakh

I like the challenge of learning new things and I needed this app on my phone for work. - Droidxuser1

@Droidxuser1: I see. Necessity is the mother of learning new skills. :-) - ruakh

As I go through this learning curve would it be possible to contact you more directly when some questions arise. - Droidxuser1

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.