1

我必须使用 ActionBarSherlock 在我的应用程序中实现选项卡。我在这里遵循这个例子。现在我想向其中一个片段添加按钮,并对其执行操作。我该怎么做 ?假设这是按钮所在的布局

public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.hello, container, false);
    }
}

我如何从视图中读取按钮?

4

3 回答 3

4
public class AFragment extends SherlockFragment {
private Button button;    
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflating the layout view  to this fragment
    View view = inflater.inflate(R.layout.hello, container, false);
    button = (Button) view.findViewById(R.id.button_id);

    button.setOnClickListener(this);

    return view;
   }
    @Override onClick() {
       switch(v.getId()_ {
          case R.id.button_id:
                       //Your logic for button goes here
                       break;
       }
    }
}
}
于 2012-11-24T23:19:51.683 回答
2

我相信这就是它的完成方式。据我所知。我还没有完全让我的工作,但不认为我的问题在这里。

    private Button button;    

    public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflater = inflate(R.layout.hello, container, false);
        button = (Button) inflater.findViewById(R.id.reminder_slider);
        button setOnItemClickListener(new OnClickListener() {
        //do stuff on item click aka @Override onClick()
        }
        return inflater;
    }
}
于 2012-11-24T22:08:40.820 回答
1
private Button button;    

    public class AFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.hello, container, false);
        //finding the button by ID inside the inflated view
        button = (Button) view.findViewById(R.id.btnid);
        //setting an event
        button.setOnItemClickListener(new OnClickListener() {

        });
        return view;
    }
于 2012-11-24T23:18:52.957 回答