Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仍然出现错误:';实现抽象方法&x27;onFocusChange(视图,布尔值)和#x27;在';OnFocusChangeListener';即使方法已经实现_Java_Android_Android Edittext_Android View_Anonymous Function - Fatal编程技术网

Java 仍然出现错误:';实现抽象方法&x27;onFocusChange(视图,布尔值)和#x27;在';OnFocusChangeListener';即使方法已经实现

Java 仍然出现错误:';实现抽象方法&x27;onFocusChange(视图,布尔值)和#x27;在';OnFocusChangeListener';即使方法已经实现,java,android,android-edittext,android-view,anonymous-function,Java,Android,Android Edittext,Android View,Anonymous Function,我正在尝试在EditText上实现OnFocusChangeListener。问题是我犯了这个错误 类“FacilityScreen”必须声明为抽象或实现 中的抽象方法“onFocusChange(视图,布尔值)” “OnFocusChangeListener” 即使方法onFocusChange已在onfocuschangeelistener中实现 这是我的密码 public class FacilityScreen extends AppCompatActivity implements

我正在尝试在
EditText
上实现
OnFocusChangeListener
。问题是我犯了这个错误

类“FacilityScreen”必须声明为抽象或实现 中的抽象方法“onFocusChange(视图,布尔值)” “OnFocusChangeListener”

即使方法
onFocusChange
已在
onfocuschangeelistener
中实现

这是我的密码

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

我做错了什么?

您需要
@重写活动中的
方法

然后像这样使用editText.setOnFocusChangeListener(this)

检查此示例


您需要在活动中重写public void onFocusChange()
方法

然后像这样使用editText.setOnFocusChangeListener(this)

检查此示例


第1版-在类级别处理侦听器

如果希望类处理侦听器,则必须告诉视图:

editText.setOnFocusChangeListener(this);
然后您必须在类级别上实现
onFocusChange
。安卓工作室应该通过点击几下就能帮你做到这一点。结果应该是:

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener {
        //your other methods

        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            // your code
        }
    }
版本2-将侦听器内联处理为匿名类


到目前为止,您实际上是在使用匿名类内联处理侦听器。如果您想在那里而不是在类级别上进行侦听,那么只需从类级别删除
onfocuschangestener
。也就是说,删除
实现AdapterView.OnItemSelectedListener、View.OnFocusChangeListener
,其余部分保持原样。

版本1-在类级别处理侦听器

如果希望类处理侦听器,则必须告诉视图:

editText.setOnFocusChangeListener(this);
然后您必须在类级别上实现
onFocusChange
。安卓工作室应该通过点击几下就能帮你做到这一点。结果应该是:

 public class FacilityScreen extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnFocusChangeListener {
        //your other methods

        @Override
        public void onFocusChange(View view, boolean hasfocus) {
            // your code
        }
    }
版本2-将侦听器内联处理为匿名类


到目前为止,您实际上是在使用匿名类内联处理侦听器。如果您想在那里而不是在类级别上进行侦听,那么只需从类级别删除
onfocuschangestener
。也就是说,删除
实现AdapterView.OnItemSelectedListener、View.OnFocusChangeListener
并保持其余部分不变。

您不需要在活动中实现它,替换

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

或者,您可以在活动本身中重写onFocusChange方法,然后在editText中进行设置,如下所示:

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(this);

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // do something
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    });
}
公共类功能屏幕扩展AppCompativeActivity实现AdapterView.OnItemSelectedListener、View.OnFocusChangeListener
{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u facilty\u屏幕);
EditText EditText=(EditText)findViewById(R.id.enter_位置);
setOnFocusChangeListener(此);
}
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
//做点什么
}
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
}
@凌驾
未选择公共无效(AdapterView父级){
});
}

您不需要在活动中实现它,请替换

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

或者,您可以在活动本身中重写onFocusChange方法,然后在editText中进行设置,如下所示:

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(this);

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // do something
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    });
}
公共类功能屏幕扩展AppCompativeActivity实现AdapterView.OnItemSelectedListener、View.OnFocusChangeListener
{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u facilty\u屏幕);
EditText EditText=(EditText)findViewById(R.id.enter_位置);
setOnFocusChangeListener(此);
}
@凌驾
public void onFocusChange(视图v,布尔hasFocus){
//做点什么
}
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
}
@凌驾
未选择公共无效(AdapterView父级){
});
}

您的类实现了一个接口。这意味着该类(在您的示例中为Activity)必须重写该类中的所有非抽象方法。 在这里,您混合了两种方法。如果您在类声明中实现了一个接口,则不需要创建内部类的新实例,如

 new OnFocusChangeListener() {
//override a method
}
没关系,保持代码不变,只需删除。在这种情况下,使用内部类方法。创建匿名实例并重写非抽象方法

implements View.OnFocusChangeListener

您的类实现了一个接口。这意味着该类(在您的示例中为Activity)必须重写该类中的所有非抽象方法。 在这里,您混合了两种方法。如果您在类声明中实现了一个接口,则不需要创建内部类的新实例,如

 new OnFocusChangeListener() {
//override a method
}
没关系,保持代码不变,只需删除。在这种情况下,使用内部类方法。创建匿名实例并重写非抽象方法

implements View.OnFocusChangeListener

谢谢@michael troger。你能帮我一下吗?谢谢你。你能帮我一下吗?谢谢你。你能帮我一下吗?谢谢你。你能帮我吗