TextView editable onLongClick -- But one small issue when BACK button pressed
<p>I have been search SO for days and have finally compiled enough answers to accomplish what I wanted.  First off, it seems to be an often asked question but not really answered (at least not the way I was looking for it).  I thought I would share my findings but I also have one small issue left that I would like to ask for help with.  Here goes:</p>

<p>I have a TextView which displays a score.  It starts at 0 and at an onClick event the score increments and updates the TextView (score is tracked as a byte - valScore).</p>

<p>onLongClick:  This was the challenge.  I want the user to be able to do a LongClick to correct/change the score.  I first found a solution that utilized another layout.xml file with just an EditText element and the OK and CANCEL buttons.  This was very cumbersome to change the score as it involved the LongClick, then the dialog opens, then you had to click on the EditText element to open the keyboard, then you enter the value, click DONE and then click OK.  I shortened it by figuring out how to open the software keyboard automatically when the dialog opened.  However, you still had to click DONE and then OK.  I didn't like this action so I continued searching.</p>

<p>Days later I came up with a bit of code and then more and with a lot of playing/hacking around I came up with the following solution:</p>

<pre><code>    // set the onLongClickListener for tvScoreHome
    tvScoreHome.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            tvScoreHome.setInputType( InputType.TYPE_CLASS_NUMBER );
            tvScoreHome.setFocusable(true);
            tvScoreHome.setFocusableInTouchMode( true );
            tvScoreHome.requestFocus();

            InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
            imm.showSoftInput(tvScoreHome, InputMethodManager.SHOW_FORCED);

            tvScoreHome.setText("");

            tvScoreHome.setOnEditorActionListener( new TextView.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_DONE) {
                        valScoreHome = Byte.valueOf( tvScoreHome.getText().toString() );

                        // This part will hide the keyboard after input
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Service.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                        tvScoreHome.setFocusable( false );
                        tvScoreHome.setFocusableInTouchMode( false );
                        tvScoreHome.setText( Byte.toString(valScoreHome) );

                        return true;
                    }

                    return false;
                }

            });

            return true;
        }
    });
</code></pre>

<p>This works EXACTLY how I want.  User performs LongClick the keyboard opens, the user enters the new value and clicks DONE.  The TextView is updated and it works great!</p>

<p>The problem arises if the user changes their mind and hits the BACK button on the device.  The keyboard closes (GOOD), but then the focus remains on the TextView instead of removing the focus like I do if the DONE button is pressed.  So if you cancel out of a change every click after that results in the keyboard opening again instead of just incrementing the score -- until you actually type a value into the keyboard and click DONE (then the regular behavior takes over again.  I need to setFocusableInTouchMode to FALSE if the BACK button is pressed.</p>

<p>The other issue is that the setText() method is executed even if the BACK button is pressed if a different value has been typed in.  Even though valScoreHome isn't updated the TextView changes.  On the next increment it goes to the correct number again, but the setText() should not execute if the BACK button is pressed.</p>

<p>Can someone help me figure this out please?</p>
