2012-11-24 100 views
1

我必须使用ActionBarSherlock在我的应用程序中实现Tabs。我按照这个例子here。现在我想将按钮添加到其中一个片段,并对其执行操作。我该怎么做 ?假设这是按钮的布局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
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; 
     } 
    } 
} 
} 
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; 
    } 
} 
+0

ADR有它。我忘了你已经在你的论点中定义了inflater。只需更换充气器即可查看 – doubleA

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; 
    } 
相关问题