How to call a method that is inside my Fragment through the onclick of a button that is also inside the fragment?

0

How to call a method that is inside my fragment by java through the onclick of a button contained in the same fragment? Here is the code:

public class FragmentMinet extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.layout_fragment_minhaconta, null);
    Button botao = (Button) view.findViewById(R.id.editarMinhasInformacoes);

    return (view);
}

public void clicar(View v){
    mostrarMsg("Titulo teste", "Mensagem teste");
}

public void mostrarMsg(String titulo, String mensagem) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setCancelable(true);
    builder.setTitle(titulo);
    builder.setMessage(mensagem);
    builder.show();
}

}

    
asked by anonymous 28.06.2016 / 14:59

1 answer

3

You need to use the OnClickListener on the button, like this:

botao.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       mostrarMsg("Titulo teste", "Mensagem teste");
   }
});

You do not need to call the click () method, you can directly call the method that displays the message.

Hugs.

    
28.06.2016 / 15:04