0

Even if I set the input type to numberdecimal or number, I have to cast the number to get the number. Then what is the use of input type in EditText views.

e.g.

int a = Integer.valueOf(editText.getText().toString());

One more thing, why do I need to use toString() with almost every views to get Text? In java, we could just getText anything from controls.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Tanvir
  • 1,642
  • 8
  • 32
  • 53

2 Answers2

0

You have to parse Text to Integer because it doesn't return int. It returns Editable formatted by input type. So if you set input type to numbers you get Editable which contains only numbers. And you have to add toString() because EditText return Editables not Strings.

Agata
  • 399
  • 3
  • 6
  • Should I use Integer.ParseInt or ValueOF for calculations – Tanvir Oct 19 '12 at 12:02
  • long weight,weight=Long.valueOf(etWeight.getText().toString()); is giving NumberFormatException...WHY ? as far as I know that Integer.parseInt return Integer object but I want a primitive type int. – Tanvir Oct 19 '12 at 12:06
  • You CAN use Integer.parseInt: int bla = Integer.parseInt(string); It works. As well as Long.parseLong() – Agata Oct 19 '12 at 12:10
  • The valueOf() returns Long or Integer, not primitive type. – Agata Oct 19 '12 at 12:12
  • Looking in AndroidDevelopers references http://developer.android.com/reference/java/lang/Integer.html#parseInt%28java.lang.String%29 and http://developer.android.com/reference/java/lang/Integer.html#valueOf%28java.lang.String%29 – Agata Oct 19 '12 at 12:19
  • You can see here... parseInt returns int and valueOf returns Integer – Agata Oct 19 '12 at 12:20
  • yes U r right, thats what I meant (though I forgot which is which) but I knew there's a difference in primitive and object type. Thanx bro. – Tanvir Oct 19 '12 at 12:23
  • 1 more thing, is there any float type for calculation, or shud I use long for decimal value? and int age=Integer.parseInt(etAge.getText().toString()); is saying "unable to parse as interger – Tanvir Oct 19 '12 at 12:28
  • In java you have short, int, long, float and double values. – Agata Oct 19 '12 at 12:33
  • if it is age than use int age = Integer.parseInt(etAge...) – Agata Oct 19 '12 at 12:37
  • Use double values double d = Double.parseDouble(string); – Agata Oct 19 '12 at 12:42
  • why on earth is still showing error for- double weight=Double.parseDouble(etWeight.getText().toString()); – Tanvir Oct 19 '12 at 12:47
  • Upppss I got it now, I didnt put value into another EditText of input type number.....sigh....Thanx bro – Tanvir Oct 19 '12 at 12:51
0

InputType is used for various purposes. For example, in a password field, it can hide the characters.

Here's the official description of every single property it can take: https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

As for the second part of your question, EditText.getText() returns Editable

This defines a common interface for all text whose content and markup can be changed (as opposed to immutable text like Strings).

So you need to use toString() to get a string out of it.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84