开发者

editbox lostfocus fires on the wrong edittext box

I have a simple app with 2 edittext boxes. When input is typed into the 1st box one set of calculations are performed. If input is put into the other instead, a different set of calc's occurs.

So, if a number is typed into the first box etBox1, and the user leaves the box, the data from the first box is used to calculate a reesult and put it into the second box. If a number is typed into the second box etBox2, the data is used to calculate a value for the first box.

I tried:

final EditText etBox1 = (EditText) findViewById(R.id.etBox1) 
final EditText etBox2 = (EditText) findViewById(R.id.etBox2)

etBox1.setOnFocusChangeListener(new View.OnFocusChangeListener()
{ 
   @override
   public void onFocusChange(View v, boolean lostfocus)
   {
       if (lostFocus == true)
       { //do my calculations....}

This fires when the focus given to etBox1 instead of waiting for the box to loose focus. This crashes the app because the user hasn't had the chance 开发者_开发技巧to input a number into the box. Any ideas why this behaves as a "hasFocus" instead of a "lostFocus"? There is no documentation available on lostFocus at Android's site.


In your question, you have this.

final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox1 = (EditText) findViewById(R.id.etBox1);

You are using the same ID when mapping the text boxes. I guess it should look like this:

final EditText etBox1 = (EditText) findViewById(R.id.etBox1);
final EditText etBox2 = (EditText) findViewById(R.id.etBox2);

Not to mention that you should also get a compilation error with your code, as you define etBox1 two times...
(Unless this is only a typo in your question, and your code actually looks different...)


According to http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html

onFocusChange is defined as public abstract void onFocusChange (View v, boolean hasFocus), so your boolean called lostFocus is named backwards and so confusing you, I'd recommend to change it to something like hasFocus.

now you should see that for if statement is the wrong way round, you should be checking if == false

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜