问题描述
我正在尝试覆盖fragment中的onbackpressed方法.但是它给了我语法错误,它应该重写接口中的super方法.为什么这样?我的片段类中也有其他方法,例如ondestroy,但没有错误.为什么要为此单独压制.我也尝试过onkeydown.同样的错误.粘贴到我的代码下面.
I am trying to override onbackpressed method inside fragment . But it gives me syntax error that it should override a super method in interface. Why so? I have other methods like ondestroy also in my fragment class but no error. Why for this backpressed alone. I tried onkeydown also. Same error. Pasting below my code.
public TestClass extends Fragment implements Testinterface
{ @Override //error must override or implement supertype method
public void onBackPressed ()
{
if (check)
Do somethin
else
getActivity().finish ()
//super.onBackPressed () // error here if I use this
}
推荐答案
您必须在片段上实现键按下,并检查键代码.onBackPress()方法可以在Activity中使用-这是片段的逻辑父级.
You have to implement on key down in fragment, check for key code. The onBackPress() method can be used in an Activity -- which is the logical parent of your fragment.
尝试一下:
frag.getView().setFocusableInTouchMode(true);
frag.getView().setOnKeyListener( new OnKeyListener(){
@Override
public boolean onKey( View v, int keyCode, KeyEvent event ){
if( keyCode == KeyEvent.KEYCODE_BACK ){
return true;
}
return false;
}
} );
这篇关于片段中的Onbackpressed在实现接口时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!