【问题标题】:ActionBarSherlock TabsActionBarSherlock 选项卡
【发布时间】:2012-12-21 20:59:47
【问题描述】:

我必须使用 ActionBarSherlock 在我的应用程序中实现选项卡。我正在关注这个例子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);
    }
}

如何从视图中读取按钮?

【问题讨论】:

    标签: android actionbarsherlock


    【解决方案1】:
    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;
          }
      }
      

      【讨论】:

      • ADR 有。我忘记了您的论点中已经定义了充气机。只需更换充气机即可查看
      【解决方案3】:
      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;
          }
      

      【讨论】: