片段间通信

时间:2015-08-19 17:14:04

标签: android

我的问题是,如果com是接口Communicater的参考,为什么我使用了getActivity(); in com =(Communicater)getActivity(); ..谢谢

public class FragmentA extends Fragment implements View.OnClickListener{
Button button;
int counter;
Communicater com;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_a,container,false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    button = (Button) getActivity().findViewById(R.id.button);
    com= (Communicater) getActivity();
    button.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    counter++;
    com.respond("this Button clicked "+counter+" times");
}

0 个答案:

没有答案