Désolé pour mon mauvais anglais, je suis un débutant d'Android et maintenant je suis bloqué.
Maintenant, ma question est de savoir comment définir les couleurs aléatoires de l'arrière-plan avec OnClickListener
. Pouvez-vous m'aider à résoudre ce problème?
J'ai une classe (Kleurenpalet.java)
package com.example.pstek.tegeltjeswijsheid; import android.support.constraint.ConstraintLayout; import android.support.constraint.solver.widgets.ConstraintWidget; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Layout; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity { private ConstraintLayout layout; private Button randombutton; int randomColor = new Kleurenpalet().getRandomColor(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = findViewById(R.id.layout); randombutton = findViewById(R.id.button); randombutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ; } }); } }
Et j'ai ma classe principale:
package com.example.pstek.randomcolor; import android.graphics.Color; import java.util.Random; public class Kleurenpalet{ private static String[] kleur = { "#39add1", // light blue "#3079ab", // dark blue "#3FD52D", // green "FFFF0000", // red ""}; public int getRandomColor() { Random rand = new Random(); int color = rand.nextInt(kleur.length); return Color.parseColor(kleur[color]); } }
4 Réponses :
Peut-être appeler setBackgroundColor
avec la couleur initialisée:
layout.setBackgroundColor(new Kleurenpalet().getRandomColor());
Ou une couleur différente à chaque fois:
layout.setBackgroundColor(randomColor);
Je ne comprends pas ce que vous essayez de faire ici:
layout.setBackgroundColor( ContextCompat.getColor( getApplicationContext(), R.color.colorPrimary ) );
Pourquoi y avez-vous laissé un espace vide? De plus, si vous analysez la couleur dans votre Kleurenpalet
, vous devez utiliser sans ContextCompat
. Définissez simplement votre couleur comme ceci:
layout.setBackgroundColor(randomColor);
ContextCompat
est pour analyser la couleur du fichier de ressources , par exemple: p>
layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
dans le code MainActivity ceci:
public class MainActivity extends AppCompatActivity { private ConstraintLayout layout; private Button randombutton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout = findViewById(R.id.layout); randombutton = findViewById(R.id.button); randombutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int randomColor = new Kleurenpalet().getRandomColor(); layout.setBackgroundColor(randomColor); } }); } }
Vous devez mettre "Random Num" et "layout set Color" dans l'événement de clic sur le bouton:
randombutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int randomColor = new Kleurenpalet().getRandomColor(); layout.setBackgroundColor(randomColor); } });