第一章:实现简易抽奖
1.1 实现步骤
1.2 程序示例
第一步:
TextView lotteryNumberTv = findViewById(R.id.lotteryView);
第二步:
Random random = new Random();
第三步:
int n = random.nextInt(100);
第四步:
lotteryNumberTv.setText(String.valueOf(n));
1.3 线程实现抽奖
示意图如下:
1.3.1 创建Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/lotteryView"
android:layout_width="420dp"
android:layout_height="377dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:gravity="center"
android:text="\?"
android:textColor="@color/colorAccent"
android:textSize="36sp"
android:visibility="visible"
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.0" />
<Button
android:id="@+id/btnLottery"
android:layout_width="148dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="32dp"
android:text="抽奖"
android:onClick="drawNumber"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
1.3.2 Java类
package com.example.mz.lottery;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import static com.example.mz.lottery.R.id.lotteryView;
public class MainActivity extends AppCompatActivity {
private boolean runningSwitch = false; //抽奖开关
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
TextView lotteryNumberTv = findViewById(R.id.lotteryView);
lotteryNumberTv.setText((String)msg.obj);
};
};
private Mythread mythread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void drawNumber(View view) {
// 第一步: 添加数字
Button btn = findViewById(R.id.btnLottery);
String btnText = btn.getText().toString();
Log.v("btn text := ", btnText);
try{
if (btnText.equals("停")) {
btn.setText("抽奖");
runningSwitch = false;
} else {
btn.setText("停");
runningSwitch = true;
new Mythread().start();
}
}catch (Exception e)
{
Log.v("drawNumber Exception = ", e.getMessage());
}
/* 主界面延时显示线程
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Button btn = findViewById(R.id.btnLottery);
TextView lotteryNumberTv = findViewById(R.id.lotteryView);
// 第二步:
Random random = new Random();
int n = random.nextInt(100);
//第三步:
lotteryNumberTv.setText(String.valueOf(n));
}
}, 300);
*/
}
private class Mythread extends Thread{
public boolean isRunning() {
return running;
}
public void setRunning(boolean running) {
this.running = running;
}
private boolean running = false;
@Override
public void run() {
super.run();
Random random = new Random();
while (true){
if (!runningSwitch){
break;
}
int n = random.nextInt(100);
handler.sendMessage(handler.obtainMessage(0, String.valueOf(n)));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
Log.v("Thread Exception := ", e.getMessage());
}
Thread.yield();
}
}
};
}