如何在片段视图上调用onClick事件

时间:2014-09-18 06:16:24

标签: java android android-fragments

我正在开发一个应用程序,其中我有一个片段类,另一个是MainActivity类。在MainActivity.java中,我动态加载Fragment类。现在,当加载Fragment类时,我想在Fragment类视图上放置一个Onclick事件。

我的代码段如下。 的 MainHandlerFragment.java

public class MainHandlerFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_detail, container, false);
        return view;
    }

    public void navigateToAnotherActivity(){    // I am not able to call this method
       startActivity(new Intent(getActivity(),  OrganizationActivity.class).putExtra("User",user));
   }
}

home_detail.xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_top_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/shadow_layout"
    android:padding="6dip"
    android:layout_margin="10dp"
    android:onClick="navigateToAnotherActivity()"       // I am not able to call this method
    >

当调用动态加载的片段视图时,无论如何都会触发onclick

4 个答案:

答案 0 :(得分:3)

尝试这种方式:

 RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.home_top_layout);

 layout.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
     startActivity(new Intent(getActivity(),  OrganizationActivity.class).putExtra("User",user));
     }
});

并删除android:onClick="navigateToAnotherActivity()"

答案 1 :(得分:1)

首先,您没有在XML中正确调用函数,更改

android:onClick="navigateToAnotherActivity()"

android:onClick="navigateToAnotherActivity"

这样做你也应该将你的功能改为

public void navigateToAnotherActivity(View v)

以便被召唤。

无论如何,Android不鼓励这样做,它可能会成为一种不推荐的调用函数的方式,所以我建议你尝试以下方法:

RelativeLayout rl = (RelativeLayout)findViewById(R.id.home_top_layout);

rl.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v) {
    navigateToAnotherActivity();
}
});

答案 2 :(得分:1)

 public class MainHandlerFragment extends Fragment
 {
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_detail, container, false);
        public void navigateToAnotherActivity()
           {    // I am not able to call this method 
                startActivity(newIntent(getActivity(),OrganizationActivity.class).putExtra("User",user));
        }
    return view;
   }

}

答案 3 :(得分:1)

试试这个

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_detail, container, false);

        RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.home_top_layout);

         rl.setOnClickListener(new View.OnClickListener()
         {
            public void onClick(View v) {
            startActivity(new Intent(getActivity(),  OrganizationActivity.class).putExtra("User",user));
          }
        });
        return view;
    }