Android: Evaluate EditText after the user finishes

2020-02-11 07:21发布

What I want I have an EditText, where the user can enter a value, such as 10.40. Once the user is done editing I am formatting his input to a currency, for instance the above mentioned value would get formatted to $10.40 (in the case of Locale.US). And I display that result in the same EditText that the user previously edited.

Problem Because I am going to format (i.e. modify) his input, I need to know when he's done (so my formatting does not interfer with his ongoing input).

What I tried TextWatcher is not an option, since it fires on every single edit, hence when the user enters one, zero, four, dot and zero (=10.40). I am currently using an OnFocusChangeListener which works as desired, BUT the problem is: I have buttons in my application and if the user clicks on a button after editing the EditText the EditText won't lose Focus, hence my formatting code never get's called ...

After this I tried messing around with the button's focus, setting it to FocusableInTouchMode - which results in the button having to be clicked twice in order to fire (which is a no go), since it gains focus the first time and get's activated the second time. After so much hacking around I was wondering if anyone got an idea as how to solve this dilemma.

Some Code

I add the following OnFocusChangeListener to my EditText whose function I described above. The EditText is encapsulated within a CurrencyTextbox (which is not really relevant):

@Override
protected OnFocusChangeListener getOnFocusChangeListener() {
    return new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                //Member variable in the class which contains an EditText
                CurrencyTextbox.this.hadFocus = true;
            } else {
                // Thes EditText has lost focus
                Log.v(TAG, "Lost focus.");
                if (CurrencyTextbox.this.hadFocus) {
                    // We previously had focus, now we lost it, format the user input!
                    // Get current value of the Textbox
                    String value = CurrencyTextbox.this.textbox.getText().toString();
                    // Formatting the user input
                    value = String.format(//Doing some formatting);
                    // Reset the had focus
                    CurrencyTextbox.this.hadFocus = false;
                }
            }
        }
    };
}

The problem however is, the above OnFocusChangelistener only get's called if my EditText loses focus, but it does NOT lose focus when I click on a button in my Activity, and as stated above, it is not feasible to set the Button's isFocusableInTouchMode to true, as it then needs to be clicked twice every time to fire.

4条回答
戒情不戒烟
2楼-- · 2020-02-11 07:27

Try using both View.setFocusableInTouchMode() and View.requestFocus(). You can use the onKeyListener() interface to detect user input and start a timer after each onKey Event to start your edit after reasonable delay.

查看更多
SAY GOODBYE
3楼-- · 2020-02-11 07:34

Yes there is definitely an issue with edit text not losing the focus once it gains. After quite a bit of googling I found that its possible to achieve the desired result by only workaround.
I had a similar requirement and I made a workaround to accomplish the above task and it works great for me.
The workaround is as follows:
Set the onClickListener for the editText and in the onClick event set a flag saying the editText was clicked.
Set the onKeyListener for the editText and get the current value of the editText in the onKey event.
Now, say you have clicked Done / Back Button in your activity/dialog. Here you can check the flags that got set when the editText was clicked. So if its true make any procedure calls you want.

EditText v = (EditText) layout.findViewById(R.id.framerate);
v.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Log.d(ExploreUIActivity.TAG, "Insided onClick");
        vClicked= true;
    }
});
v.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Log.d(ExploreUIActivity.TAG, "Inside onKeyListener");
        value = ((EditText) v).getText().toString();
        return false;
    }
});


Now for the back/done button set the onClickListener and check whether the flag was set or not

back.setOnClickListener(new OnClickListener() {
    public onClick(View v) {
        //check the flag
        //if set do some stuff
    });
查看更多
神经病院院长
4楼-- · 2020-02-11 07:37

what is the problem ur getting? need to include setOnFocusChangeListener with ur editfield. just check it. and have to override the onFocusChange.

plz check for that...

hereenter link description here

查看更多
【Aperson】
5楼-- · 2020-02-11 07:40

I did some research and amongst other things I found this thread. It elaborates upon a similar issue that you're having (of the button not gaining focus). Try the following: On your buttons, set setFocusable(true) (NOT setFocusableInTouchMode! As this spawns the annoying behaviour you stated in your OP), then set the following OnClickListener on your Buttons:

    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO workaround because of android issue nr. 2705
            button.requestFocusFromTouch();
            // Do some stuff to handle OnClick
        }
    };

But they should probably just fix the focus stuff for buttons, the current behaviour really does cause some issues (like yours).

查看更多
登录 后发表回答