I'm trying to come up with a scalable way to link two edit text boxes together for unit conversion. The user would enter a value in either of the boxes and the converted value would show up in the other.
I would normally just make it so when one EditText was edited, it would get the input, pass it through a method to convert the value and set the text of the other field.
The problem is I have a lot of these pairs and each pair is used to convert a different kind unit. If I used a TextWatcher for each box, you can see where that would start to get out of hand.
I thought about extending the TextWatcher so I could pass it the EditText View and its partner's view, but I'm not sure how to pass it what kind of conversion method needs to be used. I could assign an int to each kind of conversion and use a switch, but that doesn't seem like a good solution to me.
Is there a better way?
This is how I would do it. Create a class, say PartnerEditTextInfo ,which contains a reference number and a EditText obj. Attach this as a tag to every EditText in your app. Set the editText in the PartnerEditTextInfo to the partner editText and have a unique reference number. Thus every EditText can get a hold of its partner.
Extend the EditText class and over ride onTextChanged() method to call a common conversion method(This can be a static class static method) . You can get the partner editText by getting the tag object of the editText whose text has changed. Then ,based on which pairs of editText(based on reference number) is changed apply required conversion formula and do the setText on both the editTexts in the pair.
Caution- You need have a way to make sure you wont get into a infinite loop, have some sort of flag to differentiate changes to editText's text made by user and made by the conversion method.
Related
Does exist a library that receives multiple edit text objects, get the text from them, and put them in new object? Instead to call getText for every field separately.
You can try Android databinding library. It help you to bind your data (as a Simple POJO) with views.
You should use RecyclerView to achieve your task, add EditText in your item layout file and in editText add addTextChangedListener listener which gives you 3 methods, in afterTextChanged save string to where you want, may be in an array.
In my current Android project, I am adding a layout file for an activity composed by a list of EditText fields and a Button in the end.
I am looking for a way to, when the button is clicked, the method associated to the click retrieve all ths fields and store them in a array of Objects (something like Object object[]).
Anyone knows if this is possible and how to do that?
You have to get a reference to each of the EditTexts and then get their current value, one by one.
I am new to the android eclipse and I have a very small question. I use a Text View and a Text Field in my application. TextView just says "Enter a number below". Then, I am going to make a button so that if the user clicks on it, it makes some things given the inserted value of the textfield. Question is, do I have to say TextView mytextview = (TextView)findViewById(R.id.tview);
on my Main Activity to create an instance of it? Because, there is no data in it unlike Text Field, it just exists. Thanks a lot
There is no need to do that if you don't need to interact with the TextView itself, getting a reference to the TextView is only used if you need to manipulate it in some way or get data from it.
In my application I have five different EditText fields, all of which are numerical, but that is not important.
What is important is that I want only two of these to be editable in one go. For example, if I type in the first field, and the then the second, all the other fields become disabled. If I were then to remove the value from the first or second field, all the other fields would become editable once again, and if I added a second value elsewhere, the cycle would repeat.
In this way, I would be able to limit the number of fields "submitted" through the code to 2.
Is there a way I could achieve this?
Yes anything can be done. You probably need to make an object that holds the 5 EditTexts. Then there are probably a number of ways to do this, but I would use a TextWatcher set on each of the 5 EditTexts. Then when one of them is focused, get the text from all 5, see if the length of text is 0, and then do logic from there to disable or enable them appropriately.
See addTextChangedListener(TextWatcher watcher) in http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher
Does anyone know if it's possible to have the adapter for an AutoCompleteTextView search a different field then what it returns to the text view to be inserted?
For example, I have a string that contains a phone number, an address, and a name. This works good for searching because I can enter any of those values to get what I want, but I only want the full name to be returned back to the TextView to be displayed?
Is this possible or does the adapter have to use the same value for both searching and returning to the AutoCompleteTextView?
Hope this makes sense, thanks for reading,
Tony
For what it's worth: I think another, possibly cleaner, option would be to set an OnItemClickListener on the AutoCompleteTextView. In the callback, you can call setText on the AutoCompleteTextView, passing in whatever string you wish to appear in the view.
This gives you the advantage that the callback indicates which item was selected (e.g., the row in the cursor result list.) Otherwise, it seems to me that you need to do a search of some kind just to find the information associated with the string that gets passed in to replaceText.
This was much simpler than I thought it would be.
All you need to do is subclass either AutoCompleteTextView or MultiAutoCompleteTextView and override the replaceText method.