简体   繁体   中英

implementing OnClickListener for a fragment on android

I have a sliding menu project and inside home layout another layout is called as a fragment :

this is the 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;
    }
}

I need to import this click listener inside my java class in order to submit a form .

//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();
                  }    
                }

But when I add the above code snippet it throws me an error on undefined methods such as findViewById , OnClickListener , onClick

The button is inside this layout 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" />

While working with Fragment , you have to inflate view.

So when you want to use any widget you have to use view object with findViewById() .

Simply Do like this...

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;
    }
}

OR try other way..

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;

     }

    }
}

You can use

rootView.findViewById(...)

in your code, because your class doesn't inherit/implement this method

first...you should findView in rootView such as

btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);

then you can finish Button's operation

        // Inflate the layout for this fragment in on create

         RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a,
                container, false);
         cd = new ConnectionDetector(getActivity());




ImageButton mButton = (ImageButton) mLinearLayout.findViewById(R.id.imageButton1);
mButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // here you set what you want to do when user clicks your button,
        // e.g. launch a new activity

        Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show();


    }
});

you can call this by view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM