【问题标题】:How can set my button a random color to the background. Android如何将我的按钮设置为背景的随机颜色。安卓
【发布时间】:2019-02-06 20:36:04
【问题描述】:

对不起,我的英语不好,我是 android 的初学者,现在我被卡住了。

现在我的问题是如何使用OnClickListener 将随机颜色设置为背景。你能帮我解决这个问题吗?

我有一堂课 (Kleurenpalet.java)

 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]);
    }

}

我有我的主要课程:

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(), ;
            }
        });
    }
}

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    在 MainActivity 代码中:

      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);
            }
         });
        }
        }
    

    【讨论】:

      【解决方案2】:

      也许用初始化的颜色调用setBackgroundColor

      layout.setBackgroundColor(randomColor);
      

      或者每次都不一样:

      layout.setBackgroundColor(new Kleurenpalet().getRandomColor());
      

      【讨论】:

        【解决方案3】:

        我不明白你在这里做什么:

        layout.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), ;
        

        你为什么在那儿留空?此外,如果您在Kleurenpalet 中解析颜色,则必须不使用ContextCompat。只需像这样设置颜色:

        layout.setBackgroundColor(randomColor);
        

        ContextCompat用于从资源文件中解析颜色,例如:

        layout.setBackgroundColor(
                ContextCompat.getColor(
                    getApplicationContext(), 
                    R.color.colorPrimary
            )
        );
        

        【讨论】:

          【解决方案4】:

          您必须在按钮点击事件中输入“随机数”和“布局设置颜色”:

          randombutton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      int randomColor = new Kleurenpalet().getRandomColor();
                      layout.setBackgroundColor(randomColor);
                  }
               });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-07-11
            • 1970-01-01
            • 2019-06-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多