gpt4 book ai didi

java - 即使已实现该方法,仍然出现错误 : 'implement abstract method ' onFocusChange(View, boolean )' in ' OnFocusChangeListener'

转载 作者:行者123 更新时间:2023-12-01 17:51:34 26 4
gpt4 key购买 nike

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

Class 'FacilityScreen' must either be declared abstract or implement abstract method 'onFocusChange(View, boolean)' in '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
}
}
});
}
}

我错了什么?

最佳答案

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

比这样使用 editText.setOnFocusChangeListener(this)

Check this example

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

关于java - 即使已实现该方法,仍然出现错误 : 'implement abstract method ' onFocusChange(View, boolean )' in ' OnFocusChangeListener',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com