0

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.

1
  • This is very vague. Why different variables? Use a list / array. Show what you have so far. Commented Mar 24, 2013 at 0:39

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

4 Comments

Excellent. And in just minutes.
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
You could simply change the above to an array of Strings, which would be much easier to deal with
@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.
0

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());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.