Android: Evaluate EditText after the user finishes editing

前端 未结 4 1585
[愿得一人]
[愿得一人] 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: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
        });
    

提交回复
热议问题