鱼C论坛

 找回密码
 立即注册
查看: 3363|回复: 0

[技术交流] kotlin for android 产生彩票随机数小案例

[复制链接]
发表于 2017-11-24 10:41:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        还是继续昨天的项目,修改activity_main.xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical">

  8.     <LinearLayout
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:orientation="horizontal"
  12.         android:layout_gravity="center_horizontal"
  13.         android:layout_marginTop="20dp">
  14.     <TextView
  15.         android:id="@+id/idtext1"
  16.         android:layout_width="50dp"
  17.         android:layout_height="wrap_content"
  18.         android:text="请"
  19.         android:layout_gravity="center"
  20.         android:textSize="15sp"
  21.         android:textColor="@color/colorLightBlue"
  22.         android:layout_marginLeft="10dp"
  23.         />
  24.     <TextView
  25.         android:id="@+id/idtext2"
  26.         android:layout_width="50dp"
  27.         android:layout_height="wrap_content"
  28.         android:text="您"
  29.         android:layout_gravity="center"
  30.         android:textSize="15sp"
  31.         android:textColor="@color/colorGreen"
  32.         android:layout_marginLeft="10dp"
  33.         />
  34.     <TextView
  35.         android:id="@+id/idtext3"
  36.         android:layout_width="50dp"
  37.         android:layout_height="wrap_content"
  38.         android:text="点"
  39.         android:layout_gravity="center"
  40.         android:textSize="15sp"
  41.         android:textColor="@color/colorBlue"
  42.         android:layout_marginLeft="10dp"
  43.         />
  44.     <TextView
  45.         android:id="@+id/idtext4"
  46.         android:layout_width="50dp"
  47.         android:layout_height="wrap_content"
  48.         android:text="击"
  49.         android:layout_gravity="center"
  50.         android:textSize="15sp"
  51.         android:textColor="@color/colorRed"
  52.         android:layout_marginLeft="10dp"
  53.         />
  54.     <TextView
  55.         android:id="@+id/idtext5"
  56.         android:layout_width="50dp"
  57.         android:layout_height="wrap_content"
  58.         android:text="按"
  59.         android:layout_gravity="center"
  60.         android:textSize="15sp"
  61.         android:textColor="@color/colorPink"
  62.         android:layout_marginLeft="10dp"
  63.         />
  64.     <TextView
  65.         android:id="@+id/idtext6"
  66.         android:layout_width="50dp"
  67.         android:layout_height="wrap_content"
  68.         android:text="钮"
  69.         android:layout_gravity="center"
  70.         android:textSize="15sp"
  71.         android:textColor="@color/colorPrimaryDark"
  72.         android:layout_marginLeft="10dp"
  73.         />

  74.     </LinearLayout>
  75.     <Button
  76.         android:id="@+id/idbutton"
  77.         android:layout_gravity="center_horizontal"
  78.         android:layout_width="wrap_content"
  79.         android:layout_height="wrap_content"
  80.         android:text="产生随机数"
  81.         android:textSize="20sp"
  82.         android:layout_marginTop="20dp"/>

  83. </LinearLayout>
复制代码

        这里用的是垂直线性布局里又嵌套了一个水平线性布局,然后底下是一个按钮,按钮就跟textview一样,就是背景色是灰色的而已。 然后修改MainActivity.kt文件:
  1. package com.example.xinwei.myapplication

  2. import android.support.v7.app.AppCompatActivity
  3. import android.os.Bundle
  4. import android.util.Log
  5. import android.widget.Toast
  6. import kotlinx.android.synthetic.main.activity_main.*
  7. import java.util.*

  8. class MainActivity : AppCompatActivity() {

  9.     var i= generateSequence(0,{it+1}).take(33)

  10.     override fun onCreate(savedInstanceState: Bundle?) {
  11.         super.onCreate(savedInstanceState)
  12.         setContentView(R.layout.activity_main)
  13.         idbutton.setOnClickListener{
  14.             var random1=generateNumber(i)
  15.             idtext1.text=random1.toString()

  16.             var random2=generateNumber(i)
  17.             while (random2==random1) {
  18.                 random2=generateNumber(i)
  19.             }
  20.             idtext2.text=random2.toString()

  21.             var random3=generateNumber(i)
  22.             while ((random3==random1)||(random3==random2)){
  23.                 random3=generateNumber(i)
  24.             }
  25.             idtext3.text=random3.toString()

  26.             var random4=generateNumber(i)
  27.             while ((random4==random1)||(random4==random2)||(random4==random3)){
  28.                 random4=generateNumber(i)
  29.             }
  30.             idtext4.text=random4.toString()

  31.             var random5=generateNumber(i)
  32.             while ((random5==random1)||(random5==random2)||(random5==random3)||(random5==random4)){
  33.                 random5=generateNumber(i)
  34.             }
  35.             idtext5.text=random5.toString()

  36.             var random6=generateNumber(i)
  37.             while ((random6==random1)||(random6==random2)||(random6==random3)||(random6==random4)||(random6==random5)){
  38.                 random6=generateNumber(i)
  39.             }
  40.             idtext6.text=random6.toString()
  41.         }
  42.     }
  43.     fun generateNumber(i:Sequence<Int>):Int{
  44.         var randomNumber=Random().nextInt(i.last())
  45.         return randomNumber
  46.     }
  47. }
复制代码

注意:代码最上面一行的包名一定要用你自己原来的,不要写我的
        这里代码很简单,我们先写一个generateNumber产生随机数的方法,然后通过在按钮点击监听事件里判断不让有重复的数,然后再把这个随机数动态设置到textview里面,点击按钮后就会随机产生6个0到33的数。效果图为:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 15:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表