Domanda

I am trying to implement an activity which converts units. There are five fields which I want to be able to convert between on the fly. I can't simply use TextWatcher because when I change a field then that changes all the other fields and cause a circular call which crashes.

I looked at OnFocusChangeListener but that only works when the focus changes whereas I want the update to occur on the fly.

I then thought I could combine the two so I have my TextWatcher and inside that I put this

public void afterTextChanged(Editable s)
        {
            if (convert_kg.hasfocus())
                {
                if (convert_kg.getText().toString().matches("")) sums=0.0;  
                else sums=Double.parseDouble(convert_kg.getText().toString());

                answer=(int)Math.round(sums*2.205);
                convert_lbs.setText(String.format("%d", answer));
                    }
         }

but I am getting an error of The method hasfocus() is undefined for the type EditText

These are my imports:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.text.Editable;
import android.text.TextWatcher;

As far as I can tell from http://developer.android.com/reference/android/widget/EditText.html it should be supported? I'm sure this is simple but any ideas?

EDIT: In case anyone was wondering all the fields are EditText

convert_kg=(EditText)findViewById(R.id.convert_kg);
È stato utile?

Soluzione

See the comments. isFocused() was the method I needed to use.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top