问题与onClickListener在片段按钮 [英] Issue with onClickListener for Button in Fragment
问题描述
我在,我想有触发父活性的方法片段类的按钮。我实现了一个接口这一点。
I have a button in a fragment class that I'd like to have trigger a method in the parent activity. I've implemented an interface for this.
我的问题是,View.onClickListener是给我以下错误:
My issue is that the View.onClickListener is giving me the following error:
这onClickListener衍生'类'匿名类'要么必须声明为抽象或实现抽象方法的onClick(视图)中的onClickListener''
'Class 'Anonymous class derived from onClickListener' must either be declared abstract or implement abstract method 'onClick(View)' in 'onClickListener''
这是奇怪,因为我实现的onClick(视图)。
Which is odd, because I'm implementing onClick(View).
下面是我的片段code:
Here is the code in my Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(com.zlaporta.chessgame.R.layout.gamedescfragment, container, false);
final Button make_move = (Button) v.findViewById(R.id.make_move);
make_move.setOnClickListener(new ***View.OnClickListener()*** {
public void OnClick(View v) {
makeMoveCallback.makeMoveMethod();
}
});
星星表示code,它的Android Studio不喜欢的部分。
The stars indicate the portion of the code that Android Studio doesn't like.
推荐答案
在一个对象使用匿名内部类做到这一点:
Do it using Anonymous inner class in an object:
//declaring OnClickListener as an object
private OnClickListener btnClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
//passing listener object to button
make_move.setOnClickListener(btnClick);
希望这将帮助:)
Hope this will help :)
这篇关于问题与onClickListener在片段按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!