I would like to know how I can get text that is in a EditText after I click a button, and this value is stored in a new variable, clear my EditText, and perform the same action again, so storing the values on different variables, and then perform a calculation, my big question is to store each value in a variable for different time, appreciate if someone can help! Thank you.
2 Answers
Button myButton = (Button)findViewById(R.id.BUTTON_ID);
EditText myEditText = (EditText)findViewById(R.id.EDITTEXT_ID);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String myText = myEditText.getText().toString();
//do whatever with myText;
myEditText.setText("");
}
});
Where BUTTON_ID and EDITTEXT_ID are the id's assigned to each respectively in your XML file
4 Comments
David Manpearl
Excellent. And in just minutes.
Robert
Sorry, my English is bad and I can not express myself directly, I'll try to explain better Every time I click a button I want to get the value of an EditText (so far so good), so that each time I perform the I click need to store the value in a different variable, that's my problem
ElefantPhace
You could simply change the above to an array of Strings, which would be much easier to deal with
SeKa
@Robert Instead of thinking about "storing the value in a different variable", it's easier to think of it as "adding the new value to a collection variable". As suggested by others, a String array or a List of Strings could be a good fit.
ArrayList listString = new ArrayList();
Button myButton = (Button)findViewById(R.id.BUTTON_ID);
EditText myEditText = (EditText)findViewById(R.id.EDITTEXT_ID);
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
listString.add(myEditText.getText().toString());
myEditText.setText("");
}
});
Get String Values
for(int i=0;i<listString.size();i++)
{
System.out.println("String values..."+listString.get(i).toString());
}