Advertisement
Sketchware

Mudar button de cores e muda o nome

Jan 9th, 2023
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.70 KB | None | 0 0
  1. final int[] colors = {Color.parseColor("#008DCD"), Color.parseColor("#FFFF5722")}; // Define as cores que serão alternadas
  2.  
  3. Button button = (Button) findViewById(R.id.button1); // Substitua "button1" pelo ID do seu botão
  4.  
  5. button.setOnClickListener(new View.OnClickListener() {
  6.     int i = 0;
  7.     @Override
  8.     public void onClick(View v) {
  9.         Button button = (Button) v;
  10.         GradientDrawable gd = new GradientDrawable();
  11.         gd.setCornerRadius(38);
  12.         gd.setColor(colors[i % 2]); // Alterna entre as duas cores definidas
  13.         button.setBackground(gd);
  14.         button.setText(i % 2 == 0 ? "ativado" : "desativado"); // Alterna entre "ativado" e "desativado"
  15.         i++;
  16.     }
  17. });
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement