• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

getting text from a textfield

Cr4a

Lurker
Nov 6, 2012
4
0
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
 
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
 
  • Like
Reactions: Cr4a
Upvote 0
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?
 
Upvote 0
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^^
 
Upvote 0
You should initialize this view

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

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

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^^
 
  • Like
Reactions: Cr4a
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones