Android: Evaluate EditText after the user finishes editing

前端 未结 4 1577
[愿得一人]
[愿得一人] 2020-12-18 08:17

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

相关标签:
4条回答
  • 2020-12-18 08:42

    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

    0 讨论(0)
  • 2020-12-18 08:43

    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.

    0 讨论(0)
  • 2020-12-18 08:45

    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
        });
    
    0 讨论(0)
  • 2020-12-18 08:55

    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).

    0 讨论(0)
提交回复
热议问题