Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 随机颜色发生器在手机抖动中的应用_Android_Android Studio - Fatal编程技术网

Android 随机颜色发生器在手机抖动中的应用

Android 随机颜色发生器在手机抖动中的应用,android,android-studio,Android,Android Studio,我已经做了一个应用程序,显示随机颜色的震动。现在我试着编一个小游戏。但现在我无法操作按钮。我的主要想法是,首先用户需要选择按钮,然后在摇动后如果颜色匹配则有赢,否则,在这里你可以找到我的代码。有人能帮我吗 下面是我的.XML文件: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.andro

我已经做了一个应用程序,显示随机颜色的震动。现在我试着编一个小游戏。但现在我无法操作按钮。我的主要想法是,首先用户需要选择按钮,然后在摇动后如果颜色匹配则有赢,否则,在这里你可以找到我的代码。有人能帮我吗

下面是我的.XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/t21"
        android:layout_width="0dp"
        android:layout_height="84dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="16dp"
        android:background="#59C4C5"
        android:text="Win or Loose"
        app:layout_constraintBottom_toTopOf="@+id/tableLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="354dp"
        android:layout_height="237dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/t"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/t1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#fed766"
                android:minWidth="88dp"
                android:text="" />

            <Button
                android:id="@+id/t2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="88dp"
                android:text=""
                android:background="#eec9d2"

                />

            <Button
                android:id="@+id/t3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="88dp"
                android:text=""
                android:background="#f07d38"

                />

            <Button
                android:id="@+id/t4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="88dp"
                android:text=""
                android:background="#c7243a"
               />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/t5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:background="#f485c7"
               />

            <Button
                android:id="@+id/t6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ff1f5d4f"

                android:text="" />

            <Button
                android:id="@+id/t7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#c6cc56a9"
                android:text="" />

            <Button
                android:id="@+id/t8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#f3227588"
                android:text="" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/t9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#f2684ab4"
                android:text="" />

            <Button
                android:id="@+id/t10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#d2e7ff"
                android:text="" />

            <Button
                android:id="@+id/t11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ff6f69"
                android:text="" />

            <Button
                android:id="@+id/t12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#bed905"
                android:text="" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/t13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#e43afee1"
                android:text="" />

            <Button
                android:id="@+id/t14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#bf30b02f"
                android:text="" />

            <Button
                android:id="@+id/t15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#a8e6cf"
                android:text="" />

            <Button
                android:id="@+id/t16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#d2d4dc"
                android:text="" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:id="@+id/t17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#ea9ed0a6"
                android:text="" />

            <Button
                android:id="@+id/t18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#d9ad0495"
                android:text="" />

            <Button
                android:id="@+id/t19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#fff4e6"
                android:text="" />

            <Button
                android:id="@+id/t20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#be9b7b"
                android:text="" />
        </TableRow>

    </TableLayout>

    <Button
        android:id="@+id/t"
        android:layout_width="0dp"
        android:layout_height="84dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:text="Choose one of the above color"
        android:background="#8b9dc3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这里是我在Gradle脚本->Build.Gradle(Module.app)->


你应该有一套固定的颜色来随机选择,否则几乎不可能猜到正确的颜色。 例如,用颜色填充一个数组,就像在文章中完成的那样

然后为数组中的每种颜色创建一个按钮。
同时将所选颜色存储在一个变量中,这样您就可以检查一个人在摇动后是否猜到了正确的颜色。

谢谢edwardbrosens,我将尝试实现此方法。但是在解决了我当前的问题之后。
package com.example.android.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.squareup.seismic.ShakeDetector;
import java.util.Random;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements ShakeDetector.Listener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        ShakeDetector shakeDetector = new ShakeDetector(this);
        shakeDetector.start(sensorManager);


    }

    @Override
    public void hearShake() {
        Random rnd = new Random();
        final TextView textView = findViewById(R.id.t);

        int color = Color.argb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        textView.setText("Random color value is "+Integer.toHexString(color)+"\n and you choose "+color);
        findViewById(android.R.id.content).setBackgroundColor(color);

    }
}

dependencies { 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.squareup:seismic:1.0.2'  ///only this line is added. 
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}