Android:用户完成编辑后评估 EditText

Android: Evaluate EditText after the user finishes editing(Android:用户完成编辑后评估 EditText)

本文介绍了Android:用户完成编辑后评估 EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要什么我有一个 EditText,用户可以在其中输入一个值,例如 10.40.用户完成编辑后,我将他的输入格式化为货币,例如,上述值将格式化为 10.40 美元(在 Locale.US 的情况下).然后我在用户之前编辑的相同 EditText 中显示该结果.

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

我尝试了什么TextWatcher 不是一个选项,因为它会在每次编辑时触发,因此当用户输入一、零、四、点和零(=10.40)时.我目前正在使用 OnFocusChangeListener ,它可以按需要工作,但问题是:我的应用程序中有按钮,如果用户在编辑 EditText 后单击按钮,则 EditText 不会失去焦点,因此我的格式化代码永远不会被调用...

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

在此之后,我尝试弄乱按钮的焦点,将其设置为 FocusableInTouchMode - 这导致按钮必须单击两次才能触发(这是不行的),因为它第一次获得焦点并获得第二次激活.经过这么多的黑客攻击,我想知道是否有人知道如何解决这个困境.

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.

一些代码

我将以下 OnFocusChangeListener 添加到我上面描述的功能的 EditText 中.EditText 封装在 CurrencyTextbox 中(实际上并不相关):

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;
                }
            }
        }
    };
}

但是问题是,上面的 OnFocusChangelistener 只有在我的 EditText 失去焦点时才会被调用,但是当我点击 Activity 中的按钮时它不会失去焦点,并且如上所述,设置 Button 的 isFocusableInTouchMode 是不可行的为真,因为它需要每次点击两次才能触发.

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.

推荐答案

我做了一些研究,发现这个线程.它详细说明了您遇到的类似问题(按钮没有获得焦点).请尝试以下操作:在您的按钮上,设置 setFocusable(true)(不是 setFocusableInTouchMode!因为这会产生您在 OP 中声明的烦人行为),然后在您的按钮上设置以下 OnClickListener:

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

这篇关于Android:用户完成编辑后评估 EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android:用户完成编辑后评估 EditText

基础教程推荐