Android - how to inflate a view with multiple EditText fields and read their values?
For my Android application, I want to have a view which allows a user to click a plus button to add EditText fields, and next to the EditText fields, I want to have minus buttons that will remove them from the view. In essence, something that is very similar to adding multiple phone numbers/email address开发者_Go百科es in the edit Contact interface on Android.
I imagine I will need to do this by inflating my main view with a separate one that contains the EditText and button I want to add each time. However, I have no idea how I will manage identifying each EditText and button with a unique ID, and thus, I have no idea how I would manage to grab the values of each EditText for saving to my database. Can someone advise me on what I need to do? Thank you.
If you have inflated a sub-layout, then you should now have a View
object.
You can then call findViewById(R.id.edit_text_1)
on that View
— assuming that you supplied IDs in the sub-layout XML.
So long as you keep track of each of the parent View
s, you can always use findViewById()
on them. Or after inflation, if you really want you can set a new, globally-unique ID on each EditText
using setId()
.
You can assign a view widget dynamically, and generate your own ID as it is assigned.
Button b = new Button(this);
b.setId(myId);
Then you can refer back to that widget.
findViewById(myId);
精彩评论