Android Fragment布局监听器

时间:2015-06-17 21:06:38

标签: java android-fragments onclicklistener

我有基于用户选择来扩展特定片段的代码。我想将onCliclListener设置为特定片段布局中的按钮,但我不知道要执行哪个java文件。我假设它是片段java文件,但我无法初始化按钮。这是我的代码目前的样子,是否有人可以帮助我理解如何将侦听器编入此代码?

    public class BlackWidowFragment extends Fragment {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.blackwidow_fragment, container, false);

        Button btn = (Button) view.findViewById(R.id.playagain_button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BlackWidowFragment.this, Question1.class);
                startActivity(intent);
            }
        });
        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

像...一样的东西。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.blackwidow_fragment, container, false);

        Button b = (Button)view.findViewById(R.id.your_button_id);
        b.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                 // do something when clicked...
              }
        });

        return view;
    }