在android上为片段实现OnClickListener

Mac*_*lor 7 java android android-layout

我有一个滑动菜单项目和内部主页布局另一个布局称为片段:

这是HomeFragment.java:

package info.androidhive.slidingmenu;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要在我的java类中导入这个click监听器才能提交表单.

//CheckFal:
            btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal);
            btnCheckFalAction.setOnClickListener(new OnClickListener() {           

                  @Override
                  public void onClick(View v) 
                  {
                      Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
                  }    
                }
Run Code Online (Sandbox Code Playgroud)

但是,当我添加上面的代码片段时,它会在未定义的方法上引发错误,例如findViewById,OnClickListener,onClick

该按钮位于此布局fragment_home.xml中

  <Button
      android:id="@+id/btnCheckFal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/relativeLayout1"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="19dp"
      android:text="@string/CheckFal" />
Run Code Online (Sandbox Code Playgroud)

Pra*_* シ 17

在使用时Fragment,您必须夸大视图.

因此,当您想要使用任何小部件时,您必须使用视图对象findViewById().

简单地这样做......

public class HomeFragment extends Fragment {

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

            btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
            btnCheckFalAction.setOnClickListener(new OnClickListener() {           

                  @Override
                  public void onClick(View v) 
                  {
                      Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
                  }    
                });

        return rootView;
    }
}
Run Code Online (Sandbox Code Playgroud)

或尝试其他方式..

public class HomeFragment extends Fragment implements OnClickListener{

    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

            btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
            btnCheckFalAction.setOnClickListener(this);

        return rootView;
    }
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     switch(v.getId()){

     case R.id.btnCheckFal :
         //your code...
     break;

    default:
        break;

     }

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 确保你导入了正确的类文件,即`import android.view.View.OnClickListener;` (2认同)