小程序开发——抽一款皮肤(抽一款安琪拉的皮肤)

准备阶段

1,面向对象——步骤化

写一个文本框 显示名字
写一个输入框
写一个文本框 显示密码
写出一个输入框
写一个按钮
写一个功能 接受用户输入的姓名
写一个功能 接收用户输入的密码
写一个功能 接收用户的点击事件
(复用性比较低)

2.面向对象——行为化

写一个类TextView 文本 颜色 字体 (用来管理文本相应的内容)
创建对象
写一个类 EditText显示输入内容
创建对象
Button
这里我们举个栗子,就拿造房子来说
造房子
人:实用工具 思想(是一个类)
张三:具体的人(对象)
连接手机打开开发者程序

LinearLayout与RelativeLayout

LinearLayout



RelativeLayout(还需要有约束条件才能达到预期效果)

因此此处使用LinearLayout更为合适

搭建页面

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <!-- 文本框   <-->


    <!--  按钮  <-->

    <TextView

        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="#2F2E2F"
        android:gravity="center"
        android:text="qqqqqqqq"
        android:textColor="#FFF"
        android:textSize="30sp"

        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:background="#d65489"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:text="开始抽奖"
        android:textColor="#fff"
        android:textSize="20sp"
        />

</LinearLayout>

实现功能

点击按钮:切换按钮的标题 开始抽奖和暂停抽奖切换
准备候选人 数组保存
开始抽奖

activity_main.java

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">


    <!-- 文本框   <-->


    <!--  按钮  <-->

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="#2F2E2F"
        android:gravity="center"
        android:text="抽一款安琪拉的皮肤"
        android:textColor="#FFF"
        android:textSize="30sp"

        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:background="#d65489"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:text="开始抽奖"
        android:textColor="#fff"
        android:textSize="20sp"
        android:onClick="start"





        />

</LinearLayout>

MainActivity.java

package com.example.luck;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    //准备候选人 数组
    String[] names = new String[]{"心灵骇客","魔法小厨娘","玩偶对对碰","如懿"};
    Timer timer;
    /*
    1.onCreate 配置
    2.onResume
    3.onPause
    4.onDestory
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //按钮点击事件
    public void start(View view) {
        //将View转化文为Button
        Button btn = (Button)view;

        //获取当前标题
        String title = btn.getText().toString();
        //判断按钮的标题
        if(title.equals("开始抽奖")){
            //设置为暂停
            btn.setText("暂停");
            //创建一个定时器
            timer = new Timer();
            //每隔一段时间去执行一个任务
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    produceOnePeople();
                }
            },0,50);
        }else{
            //设置为开始抽奖
            btn.setText("开始抽奖");
            //关闭计时器
            timer.cancel();
        }

        produceOnePeople();
    }


    //产生一个随机皮肤
    public void produceOnePeople(){
        //产生一个随机数
        Random random = new Random();
        int index = Math.abs(random.nextInt()) % names.length;

        //从数组里取出这个皮肤
        String name = names[index];

        //将名字显示到文本框上
        TextView tv = findViewById(R.id.tv_name);
        tv.setText(name);
    }
}

最终成功



你抽到了那款皮肤呢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容