実現したいこと
androidのアプリで自分で設定した範囲,好きな数だけランダムに表示されるソフトを作っています。サイトを参考にしながら頑張ったのですが上手くいきませんでした。詳しく教えていただけると嬉しいです。
該当のソースコード
java
1package com.e.random; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.text.SpannableStringBuilder; 6import android.view.View; 7import android.widget.Button; 8import android.widget.EditText; 9import android.widget.TextView; 10 11import java.util.ArrayList; 12import java.util.Collections; 13 14public class MainActivity extends AppCompatActivity { 15 private boolean flag = false; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 TextView textView1 = findViewById(R.id.textView1); 23 textView1.setText("Minimum Number"); 24 25 TextView textView2 = findViewById(R.id.textView2); 26 textView2.setText("Maximum Number"); 27 28 TextView textView3 = findViewById(R.id.textView3); 29 textView3.setText("Extraction Number"); 30 31 EditText editText1 = findViewById(R.id.editText1); 32 SpannableStringBuilder sb1 = (SpannableStringBuilder) editText1.getText(); 33 final String str1 = sb1.toString(); 34 35 EditText editText2 = findViewById(R.id.editText2); 36 SpannableStringBuilder sb2 = (SpannableStringBuilder) editText2.getText(); 37 final String str2 = sb2.toString(); 38 39 EditText editText3 = findViewById(R.id.editText3); 40 SpannableStringBuilder sb3 = (SpannableStringBuilder) editText3.getText(); 41 final String str3 = sb3.toString(); 42 final int z = Integer.parseInt(str3); 43 44 Button button = findViewById(R.id.GENERATE); 45 button.setOnClickListener(new View.OnClickListener() { 46 @Override 47 public void onClick(View v) { 48 if (flag){ 49 ArrayList<Integer> list = new ArrayList<>(); 50 for (int i = Integer.parseInt(str1);i > Integer.parseInt(str2);i++) { 51 list.add(i); 52 } 53 Collections.shuffle(list); 54 55 int[] answer = new int[z]; 56 for (int i = 0; i < answer.length; i++) { 57 answer[i] = list.get(i); 58 } 59 TextView textView4 = findViewById(R.id.textView4); 60 textView4.setText("" + answer); 61 } 62 } 63 }); 64 65 } 66}
↑MainActivity.java
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="#000000" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:id="@+id/textView1" 12 android:layout_width="136dp" 13 android:layout_height="23dp" 14 android:layout_marginStart="32dp" 15 android:layout_marginTop="128dp" 16 android:textColor="#ffffff" 17 android:textSize="7pt" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="parent" /> 20 21 <TextView 22 android:id="@+id/textView2" 23 android:layout_width="118dp" 24 android:layout_height="23dp" 25 android:layout_marginStart="32dp" 26 android:text="Maximum Number" 27 android:textColor="#ffffff" 28 android:textSize="7pt" 29 app:layout_constraintStart_toStartOf="parent" 30 tools:layout_editor_absoluteY="183dp" /> 31 32 <TextView 33 android:id="@+id/textView3" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:layout_marginStart="32dp" 37 android:layout_marginEnd="88dp" 38 android:text="Extraction Number" 39 android:textColor="#ffffff" 40 android:textSize="7pt" 41 app:layout_constraintEnd_toStartOf="@+id/editText3" 42 app:layout_constraintStart_toStartOf="parent" 43 tools:layout_editor_absoluteY="238dp" /> 44 45 <EditText 46 android:id="@+id/editText1" 47 android:layout_width="120dp" 48 android:layout_height="32dp" 49 android:layout_marginEnd="32dp" 50 android:background="#888888" 51 android:ems="10" 52 android:inputType="number" 53 android:singleLine="false" 54 android:text=" Type here" 55 android:textColor="#ffffff" 56 app:layout_constraintEnd_toEndOf="parent" 57 tools:layout_editor_absoluteY="117dp" /> 58 59 <EditText 60 android:id="@+id/editText2" 61 android:layout_width="120dp" 62 android:layout_height="32dp" 63 android:layout_marginEnd="32dp" 64 android:background="#888888" 65 android:ems="10" 66 android:inputType="number" 67 android:text=" Type here" 68 android:textColor="#ffffff" 69 app:layout_constraintEnd_toEndOf="parent" 70 tools:layout_editor_absoluteY="174dp" /> 71 72 <EditText 73 android:id="@+id/editText3" 74 android:layout_width="120dp" 75 android:layout_height="32dp" 76 android:layout_marginEnd="32dp" 77 android:background="#888888" 78 android:ems="10" 79 android:inputType="number" 80 android:text=" Type here" 81 android:textColor="#ffffff" 82 app:layout_constraintEnd_toEndOf="parent" 83 tools:layout_editor_absoluteY="228dp" /> 84 85 <Button 86 android:id="@+id/GENERATE" 87 style="@style/Widget.AppCompat.Button.Colored" 88 android:layout_width="wrap_content" 89 android:layout_height="wrap_content" 90 android:layout_marginStart="161dp" 91 android:layout_marginEnd="148dp" 92 android:background="@drawable/button_state" 93 android:text=" ⚠ GENERATEE ⚠ " 94 app:layout_constraintEnd_toEndOf="parent" 95 app:layout_constraintHorizontal_bias="1.0" 96 app:layout_constraintStart_toStartOf="parent" 97 tools:layout_editor_absoluteY="299dp" /> 98 99 <TextView 100 android:id="@+id/textView4" 101 android:layout_width="0dp" 102 android:layout_height="wrap_content" 103 android:layout_marginStart="32dp" 104 android:layout_marginTop="24dp" 105 android:layout_marginEnd="32dp" 106 android:layout_marginBottom="32dp" 107 android:text="" 108 android:textColor="#ffffff" 109 android:textSize="12pt" 110 app:layout_constraintBottom_toBottomOf="parent" 111 app:layout_constraintEnd_toEndOf="parent" 112 app:layout_constraintStart_toStartOf="parent" 113 app:layout_constraintTop_toBottomOf="@+id/GENERATE" /> 114 115 <android.support.constraint.Guideline 116 android:id="@+id/guideline" 117 android:layout_width="wrap_content" 118 android:layout_height="wrap_content" 119 android:orientation="vertical" 120 app:layout_constraintGuide_begin="20dp" /> 121 122</android.support.constraint.ConstraintLayout> 123```↑layout 124 125 126### 補足情報(FW/ツールのバージョンなど) 127 128Android Studio
あなたの回答
tips
プレビュー