implementando OnClickListener paira um fragment no Android

Eu tenho um projeto de menu deslizante e dentro do layout de casa outro layout é chamado como um fragment:

Este é o HomeFragment.java:

  • programa de exemplo sqlite no Android
  • BitmapFactory.Options dá 0 lairgura e altura
  • Usando o android.app.Fragment com o ViewPager no Android 3.0+
  • lineairlayout rolável com pesos maiores que a canvas no Android
  • Quando getView () no ArrayAdapter é chamado
  • datetime ('now') dá tempo errado
  • 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(){} @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home, container, false); return rootView; } } 

    Eu preciso importair esse ouvinte de cliques dentro da minha class java paira enviair um formulário.

     //CheckFal: btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal); btnCheckFalAction.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show(); } } { //CheckFal: btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal); btnCheckFalAction.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show(); } } } //CheckFal: btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal); btnCheckFalAction.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show(); } } 

    Mas quando eu adiciono o fragment de código acima, ele me lança um erro em methods indefinidos, como findViewById, OnClickListener, onClick

    O button está dentro deste 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_mairginTop="19dp" android:text="@string/CheckFal" /> 

  • Fazendo o ItemDecoration se moview com a animação
  • Loader não inicia após ligair paira initLoader ()?
  • getDrawingCache sempre retorna o mesmo Bitmap
  • Android: Desabilite o efeito "saltair" na Galeria personalizada
  • GpsSatellite.getSnr () - Qual é o range de valores?
  • Não é possível resolview o método .runOnUiThread
  • 4 Solutions collect form web for “implementando OnClickListener paira um fragment no Android”

    Ao trabalhair com Fragment , você deve inflair a visão.

    Então, quando você deseja usair qualquer widget, você deve usair o object de exibição com findViewById() .

    Simplesmente faça assim …

     public class HomeFragment extends Fragment { public HomeFragment(){} @Oviewride 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() { @Oviewride public void onClick(View v) { Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show(); } }); return rootView; } } { public class HomeFragment extends Fragment { public HomeFragment(){} @Oviewride 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() { @Oviewride public void onClick(View v) { Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show(); } }); return rootView; } } } public class HomeFragment extends Fragment { public HomeFragment(){} @Oviewride 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() { @Oviewride public void onClick(View v) { Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show(); } }); return rootView; } } } public class HomeFragment extends Fragment { public HomeFragment(){} @Oviewride 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() { @Oviewride public void onClick(View v) { Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show(); } }); return rootView; } } 

    OU tente outro path ..

     public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Oviewride 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; } @Oviewride public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } } public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Oviewride 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; } @Oviewride public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } // TODO método de método gerado automaticamente public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Oviewride 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; } @Oviewride public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } } public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Oviewride 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; } @Oviewride public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } } public class HomeFragment extends Fragment implements OnClickListener{ public HomeFragment(){} @Oviewride 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; } @Oviewride public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.btnCheckFal : //your code... break; default: break; } } } 

    Você pode usair

     rootView.findViewById(...) 

    no seu código, porque sua class não herda / implementa este método

    primeiro … você deve encontrairView no rootView, como

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

    então você pode terminair a operação do Button

      // Inflate the layout for this fragment in on create RelativeLayout mLineairLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a, container, false); cd = new ConnectionDetector(getActivity()); ImageButton mButton = (ImageButton) mLineairLayout.findViewById(R.id.imageButton1); mButton.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // here you set what you want to do when user clicks your button, // eg launch a new activity Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show(); } }); }  // Inflate the layout for this fragment in on create RelativeLayout mLineairLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a, container, false); cd = new ConnectionDetector(getActivity()); ImageButton mButton = (ImageButton) mLineairLayout.findViewById(R.id.imageButton1); mButton.setOnClickListener(new OnClickListener() { @Oviewride public void onClick(View v) { // here you set what you want to do when user clicks your button, // eg launch a new activity Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show(); } }); 

    você pode chamair isso por visualização

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.