我正在尝试在OnFocusChangeListener上实现EditText。问题是我遇到此错误


  “ FacilityScreen”类必须声明为抽象或实现
  抽象方法'onFocusChange(View,boolean)'在
  'OnFocusChangeListener'


即使方法onFocusChange已在OnFocusChangeListener中实现。

这是我的代码

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facilty_screen);

        EditText editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View view, boolean hasfocus) {
                if(hasfocus){
                     //do something
                }
            }
        });
    }
}


我怎么了?

最佳答案

您需要在活动中使用@Override public void onFocusChange()方法

比这样使用editText.setOnFocusChangeListener(this)


  检查这个例子


public class FacilityScreen extends AppCompatActivity implements  View.OnFocusChangeListener{

    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_facilty_screen);
        editText = (EditText) findViewById(R.id.enter_location);
        editText.setOnFocusChangeListener(this);

    }


    @Override
    public void onFocusChange(View view, boolean b) {
        if(view==editText){
            // perfom your action here
        }
    }
}

10-07 20:05