getting text from a textfield


  1. Cr4a

    Cr4a New Member

    hiho, we are trying to get the text from a textfield and convert this string to a int/double value but it doesnt work. here is our code

    public void Addieren()
    {
    string hallo = zahl1.getText();
    double d1 = Double.parseDouble(zahl1.getText());

    double d2 = Double.parseDouble(zahl2.getText());

    double ergebnis = d1-d2;
    ausgabe.setText = ergebnis.toString();

    }

    zahl1, zahl2 are the two textfield ids.

    thx in advance

    Advertisement
    :
  2. Ride2Live

    Ride2Live Member

    There is no double type representstion for String type.
    You should use byteArray
    string hallo1 = zahl1.getText();
    string hallo2 = zahl2.getText();
    byte [] bytes1 = hallo1.getBytes();
    byte [] bytes2 = hallo2.getBytes();

    if text encoding - UTF-8, every byte in array its a char representation in integer.

    You cant simply convert String to Double or Int

    Tell me what you want to do with this strings?
    Math operations are not allowed for strings
    Cr4a likes this.
  3. Cr4a

    Cr4a New Member

    So with this operations i can do the 2nd part i want to do, thanks,
    but the main part which the question was about (sorry for not writing that in the first post), was programming a simple calculator.
    So i need double values. Is there any possibility to get double values out of textfields, or do i have to create other objects?
  4. Ride2Live

    Ride2Live Member

    Thats realy easy )
    double d1 = Double.parseDouble(zahl1.getText().ToString());
    Cr4a likes this.
  5. Cr4a

    Cr4a New Member

    well if it was that easy I'd be very happy.
    I still get the error "zahl1 cannot be resolved"

    Maybe i failed at creating the textfield?

    <EditText android:id="@+id/zahl1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="@string/zahl_1" />

    maybe a missing attribute? or do i have to declare a local variable? or just some completely stupid things? xD
    thanks in advance again^^
  6. Ride2Live

    Ride2Live Member

    You should initialize this view

    EditText myEditText = (EditText)findViewById(R.id.zahl1);

    then call it:
    myEditText.getText().ToString();

    Cr4a likes this.
  7. Cr4a

    Cr4a New Member

    thanks alot u saved my day^^
Loading...

Share This Page