遷移してくるたびにボタンをランダム配置したい

アプリ開発で暗証番号を入力させる画面を作っていて、
入力させる画面を表示するたびに番号を入力させるボタンをランダムに配置する機能を付けたいなと思い実装しようとしたのですが、実装の方法がわかりません。
※以下の画像が番号を入力させるために配置したボタンです。

イメージ説明

9つのボタンの位置関係は変えずに、0~9までのボタンの位置をランダムに配置したいです。

該当のソースコード

<Button android:id="@+id/button_1" android:layout_width="60dp" android:layout_height="60dp" android:background="@color/white" android:backgroundTint="#e5e5e5" android:text="1" android:textAlignment="center" android:textColor="#ffffff" android:textSize="28sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/button_2" app:layout_constraintHorizontal_bias="0.93" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.475" app:rippleColor="@color/white" app:strokeColor="@color/white" /> <Button android:id="@+id/button_2" android:layout_width="60dp" android:layout_height="60dp" android:background="@color/white" android:backgroundTint="#e5e5e5" android:text="2" android:textAlignment="center" android:textColor="#ffffff" android:textSize="28sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.475" app:rippleColor="@color/white" app:strokeColor="@color/white" />

このように0~9までのボタンをXML上で配置し、それぞれに持たせたidを使って、

protected void passWord() { Button button0 = findViewById(R.id.button_0); Button button1 = findViewById(R.id.button_1); Button button2 = findViewById(R.id.button_2); Button button3 = findViewById(R.id.button_3); Button button4 = findViewById(R.id.button_4); Button button5 = findViewById(R.id.button_5); Button button6 = findViewById(R.id.button_6); Button button7 = findViewById(R.id.button_7); Button button8 = findViewById(R.id.button_8); Button button9 = findViewById(R.id.button_9); Button cancel = findViewById(R.id.cancel); button0.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "0"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button1.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "1"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button2.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "2"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button3.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "3"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button4.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "4"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button5.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "5"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button6.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "6"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button7.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "7"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button8.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "8"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); button9.setOnClickListener(v -> { count += 1; if (count < 5) { inputPassword += "9"; Log.d(TAG, inputPassword); TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); } }); cancel.setOnClickListener(v -> { Log.d(TAG, "Previous Pass:" + inputPassword); inputPassword = ""; count = 0; TextView textView = findViewById(R.id.password); textView.setText(toAst(inputPassword)); }); }

このように入力させていく感じです。
どなたかわかる方いましたら、回答をお願いします。
長文失礼しました。

コメントを投稿

0 コメント