Update the value obtained in an Edittext

Asked

Viewed 1,107 times

2

Next, I’m having a problem taking the value in an Edittext and displaying it in a Textview. The value displayed when using a string, is "null", and when using int, is 0. I believe this happens because the value being collected, is what appears before the typed, IE, nothing!

I wonder if there is any way to monitor the value typed in Edittext so that when it is changed, the button collects the new value, not the previous one, if that is the problem.

Um exemplo do valor obtido

The code of the class responsible for the calculation is here.

Edit.: Just call the method inside the button Click event.

btnCalc = (Button)findViewById(R.id.btnCalc);
                        btnCalc.setOnClickListener (new View.OnClickListener()
                    { 
                        public void onClick(View v)
                        {      
                                Calculate(); //este método
                                result = "#" + rst1 + remainderR + rst2 + remainderG + rst3 + remainderB;
                                txtResult.setText(result);     
                        }
                    });
  • 3

    edit and enter the code you made to calculate

  • All right, I updated.

  • @You have to start using debug, you eliminate numerous errors with this practice. The way it is, it’s really hard for someone to help you. Suggestion: How to use debug in Eclipse?

  • 1

    you didn’t call the calculation method

  • Friend you have to do the calculation inside onClick, otherwise it will not be considered the function that calculates.

  • But onClick is inside onCreate, which is totally unviable, since the method does not accept very long codes.

  • I will start using debug, although I believe that is not the case. Thank you.

  • 2

    Leo, simply call the function Calculate() onClick before displaying the result. It is not to put the function inside all.

  • See here: http://pastebin.com/bT4i9MMk

  • 1

    Thank you, Jorge. That’s all that was missing. I apologize to you for the silly question, but I started programming recently and I end up forgetting things like this. :/

  • 2

    We are here to help, always try to debug to know what is going wrong. Over time you will gain practice :)

  • 3

    Hi @Leonardosaldanha! Since the question was completed, you could edit the question, to put the code snippet problem, without the need for people to access an external link?

  • All right, @carlosrafaelgn.

Show 8 more comments

2 answers

4


The variables are NULL because they are only initialized in the method Calculate(), that is not being called when the button is clicked.

1

I took this code from another forum, I believe it might be useful to you:

final EditText edittext = (EditText) findViewById(R.id.edittext);
    edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
          return true;
        }
        return false;
    }
});

Basically it monitors an Edit and when the user clicks enter returns the information.

Follow the link to the credits: http://www.devmedia.com.br/forum/como-acessar-funccoes-do-campo-edittext-onenter-setfocus/446366

Browser other questions tagged

You are not signed in. Login or sign up in order to post.