3

I am trying to bind the text of EditText object to a local for the view variable, but probably I do not understand how.

  • How could bind EditText object with a variable in Java - Android 4.0.3 ?
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
Milosh Silk
  • 58
  • 1
  • 6

3 Answers3

7

Make an variableChangedListener like so

In the listener, add setText(variable) for the EditText

Community
  • 1
  • 1
Gjordis
  • 2,540
  • 1
  • 22
  • 32
1

You can bind EditText to the local variable of type EditText

EditText text=new EditText(this);

text=youredittex;
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Hello. Could I bind it to a string ? – Milosh Silk Feb 25 '13 at 09:28
  • 3
    @MiloshSilk ok I'll ask you simple thing, is it possible to lift a hill with your finger...How could you ask that....You can't even store List obect in a string how could you ask this..? – Pragnani Feb 25 '13 at 09:33
  • 3
    Probably he were asking about the text property, not the object itself. –  Feb 25 '13 at 09:42
  • Wow. That was fast.. Thank You. Yes. I wish to know if there is a way to bing it's property text to a string variable as well, or it is better to ask it in separate question ? – Milosh Silk Feb 25 '13 at 09:45
  • 1
    String edittextvalue=youredittext.getText().toString() ? is that something you want..? – Pragnani Feb 25 '13 at 09:50
  • Yes. But is it binded just this way ? – Milosh Silk Feb 25 '13 at 09:53
  • @MiloshSilk it will store the text entered in edittext to your string variable... – Pragnani Feb 25 '13 at 09:55
  • but when I change the variable value - will it change the text of EditText object ? – Milosh Silk Feb 25 '13 at 10:03
  • @Milosh Silk ..No it won't... You need to set text for EditText when the string changes – Pragnani Feb 25 '13 at 10:06
  • 3
    @Pragnani, I feel you didn't understand what MiloshSilk meant by binding. What you are talking about is "setting" the value of a text, "binding" is different. Binding is when you link a variable and a control together so that when the control's change, the variable will also, and when the variable change, the control will change. This is really helpful in saving you the time of having to add handlers, etc. – Rafid Nov 15 '14 at 16:40
  • If you are into web development, AngularJS is a good example of binding. Have a look at this: https://docs.angularjs.org/guide/databinding – Rafid Nov 15 '14 at 16:44
-1

You can do by this:

EditText bindText=new EditText(context);

bindText=anyThingHere;
Anurag Shukla
  • 195
  • 2
  • 6