安卓:评估的EditText用户完成编辑后, [英] Android: Evaluate EditText after the user finishes editing

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

问题描述

我要什么 我有一个EditText,用户可以在其中输入一个值,如10.40。一旦用户完成编辑我格式化他的投入到货币,比如上面提到的价值将得到格式化为$ 10.40(中Locale.US的情况下)。和我在同一个EditText上,用户previously编辑的显示效果。

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是不是一种选择,因为它触发对每一个编辑,因此当用户输入1,零,四个,点和零(= 10.40)。我目前使用的达到目标工作的OnFocusChangeListener,但问题是:我在我的应用程序按钮,如果用户在编辑的EditText后点击一个按钮的EditText不会失去焦点,因此我的格式code从不得到的所谓...

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.

有些code

我添加以下OnFocusChangeListener我的EditText其功能我上面描述。该EditText上是一种货币文字封装内(这是不是真正相关):

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失去焦点,但它并没有失去焦点,当我点击我的活动按钮,并且如上所述,这是不可行的设置按钮的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.

推荐答案

我做了一些研究,除其他事项外,我发现<一href="http://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=2705">this线。它详尽阐述,你有(的按钮没有获得焦点)类似的问题。请尝试以下操作:在您的按钮,设置setFocusable(真),然后设置下列OnClickListener您的按钮(未setFocusableInTouchMode由于此产卵你在OP规定的令人讨厌的行为!):

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆