?xml version="1.0" encoding="utf-8"?> LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="小さい順にタップ" android:textSize="40sp" /> Button android:id="@+id/b0" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onButton" android:text="○" android:textSize="30sp" /> Button android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onButton" android:text="○" android:textSize="30sp" /> Button android:id="@+id/b2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onButton" android:text="○" android:textSize="30sp" /> Button android:id="@+id/b3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:onClick="onButton" android:text="○" android:textSize="30sp" />
package com.example.kozaburou.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Arrays; import java.util.Collections; import java.util.List; public class MainActivity extends AppCompatActivity { String[] QUIZ = {"0","2","6","9"}; // 問題 int tap = 0; // 現在の正解数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 出題(シャッフル)////////////////////////// List list = Arrays.asList(QUIZ.clone()); Collections.shuffle(list); ((Button)findViewById(R.id.b0)).setText(list.get(0)); ((Button)findViewById(R.id.b1)).setText(list.get(1)); ((Button)findViewById(R.id.b2)).setText(list.get(2)); ((Button)findViewById(R.id.b3)).setText(list.get(3)); } // ボタンチェック ///////////////////////// public void onButton( View v){ // タップされたボタンの文字を取得 String text = ((Button)v).getText().toString(); // 正解処理 ( 問題と比較 ) if( text.equals(QUIZ[tap])){ v.setEnabled(false); // ボタンをクリック不可 tap++; // 正解数UP ((TextView)findViewById(R.id.tv)).setText( tap+"問正解!!"); // 正解表示 if(tap >= 4){ ((TextView)findViewById(R.id.tv)).setText("全問正解"); } } else { ((TextView)findViewById(R.id.tv)).setText("不正解"); ((Button)findViewById(R.id.b0)).setEnabled(false); ((Button)findViewById(R.id.b1)).setEnabled(false); ((Button)findViewById(R.id.b2)).setEnabled(false); ((Button)findViewById(R.id.b3)).setEnabled(false); } } }