我必须使用ActionBarSherlock在我的应用程序中实现Tabs。我正在关注此示例here。现在我想向其中一个片段添加按钮,并对其执行操作。我该怎么做 ?假设这是按钮
的布局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);
}
}
如何从视图中读取按钮?
答案 0 :(得分: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;
}
}
}
}
答案 1 :(得分: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;
}
}
答案 2 :(得分: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;
}