2012-11-24 98 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; 
    } 
相關問題