Problem while doing method that checks which button was clicked

Asked

Viewed 386 times

2

I am creating a screen with 3 buttons in which each button clicked will need to be redirected to a different site. I implemented this code, there is no error, I can even click the button, only when the page is loaded there is an error in the application and it closes alone. What I’m doing wrong in this way? erro

  • What mistake does it make? Post the log of errors.

  • If by clicking the button the page opens then the error is not on the button.

  • Another mistake I realized, is that even without pressing the button he enter the links, one behind the other. It’s as if these conditions had not been worth

  • kkkk Now that I stopped to look no click function there

  • All this is executed when Oncreateview() is started.

  • I’ll show you how to do a function like that.

  • Blz Bruno, I’m on hold

Show 2 more comments

1 answer

5


This code you posted does not have a click function, it is running all this when the OnCreateView() is started. Try to do so:

In your class, right after the extends Cronograma, add implements View.OnClickListener.

And in the OnCreateView() do this:

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

    View view = inflater.inflate(R.layout.solicitacoes, container, false);
    Button button1 = view.findViewById(R.id.id_do_button1);
    Button button2 = view.findViewById(R.id.id_do_button2);
    Button button3 = view.findViewById(R.id.id_do_button3);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

  return view;
 }

So to finish just create (the correct term I think is to implement) the function of OnClick:
Obs: I gave a simplified in its function using switch instead of if, taking into account that you just want to catch the event of clicking the buttons on this classe:

@Override
public void onClick(View v) {
   String url = "";
   switch (v.getId()) {
     case R.id.button:
       url = "sua url aqui";
       break;
     case R.id.button:
       url = "sua url aqui";
       break;
     default: // padrão, seria igual o else aqui
       url = "url padrão aqui";
       break;

     Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse(url))...
     startActivity(it);
   }
}

  • Bruno so mais uma duvida, nessa linha: View view = inflater.inflate(R.layout.solicitacoes, container, false);
 Button seuButton = view.findViewById(R.id.id_do_seu_button);
 seuButton.setOnClickListener(this) eu coloco o id de cada botaow(no meu caso 3 botoes)?

  • Perfetio Bruno, mto obgdo. the application is running smoothly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.