<第一款隨機選號App (尾牙,抽獎超實用) Random number>
App實際效果影片如下 As the following video :
【activity_main.xml】
As the following code:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:theme="@style/Theme.AppCompat.Light.NoActionBar" tools:context=".MainActivity">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="10" android:background="#ff0" android:gravity="center" android:text="隨機產生號碼1~99" android:textColor="#000000" android:textSize="27dp" android:textStyle="bold" tools:ignore="TextContrastCheck" />
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="20" android:orientation="vertical" android:paddingHorizontal="12dp" android:paddingTop="12dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第n組"/>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/num0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/> <TextView android:id="@+id/num1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/> <TextView android:id="@+id/num2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/>
<TextView android:id="@+id/num3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/> <TextView android:id="@+id/num4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/> <TextView android:id="@+id/num5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#fff" android:textStyle="bold" android:gravity="center" android:textSize="20sp" android:background="@drawable/number_bg"/> </LinearLayout> </LinearLayout>
<RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="60"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按下隨機產生 Click" android:textColor="#000000" android:background="#ffd700" android:textSize="27dp" android:textStyle="bold" android:layout_alignParentBottom="true" android:layout_marginHorizontal="12dp" android:onClick="lottery"/>
</RelativeLayout>
</LinearLayout>
|
As the following code:
import java.util.HashSet;
public class MainActivity extends AppCompatActivity { private TextView num0,num1,num2,num3,num4,num5;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
num0 = findViewById(R.id.num0); num1 = findViewById(R.id.num1); num2 = findViewById(R.id.num2); num3 = findViewById(R.id.num3); num4 = findViewById(R.id.num4); num5 = findViewById(R.id.num5);
}
public void lottery(View view) { HashSet<Integer> set = new HashSet<>(); while (set.size()<6){ set.add((int)(Math.random()*99+1)); }
int i = 0; int[] lottery = new int[6]; for (Integer num : set){ lottery[i] = num; i++; }
num0.setText("" + lottery[0]); num1.setText("" + lottery[1]); num2.setText("" + lottery[2]); num3.setText("" + lottery[3]); num4.setText("" + lottery[4]); num5.setText("" + lottery[5]);
} }
|