Android生成随机数的方法实例
更新时间:2021年03月22日 11:13:02 作者:你先去买饭
这篇文章主要为大家详细介绍了Android生成随机数的方法实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android生成随机数的具体代码,供大家参考,具体内容如下
java文件
package com.example.my_xm_bw_luowei; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.Random; public class MainActivity extends AppCompatActivity { private Button bt1; private EditText et1; private EditText et2; private TextView tv; private int i=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1); et2 = (EditText) findViewById(R.id.et2); tv = (TextView) findViewById(R.id.tv); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; String trim2 = et2.getText().toString().trim(); String s = ""; Random ran =new Random(System.currentTimeMillis()); Log.d("aaa", "onClick: "+i); for (int i = 0 ; i <1; i++) { s = s + "\n" + ran.nextInt(Integer.parseInt(trim2)); } tv.setText(s+""); if (i==10){ tv.setText("直接让高金玉上 嘻嘻嘻"); } } }); } }
xml文件
<?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" > <Button android:text="高金玉" android:id="@+id/bt1" android:background="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <EditText android:id="@+id/et2" android:hint="0-n" android:layout_width="match_parent" android:layout_height="wrap_content"></EditText> <TextView android:textColor="#DD0D0D" android:textSize="50dp" android:id="@+id/tv" android:text="0" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </LinearLayout>
效果图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android中使用ZXing生成二维码(支持添加Logo图案)
ZXing是谷歌的一个开源库,可以用来生成二维码、扫描二维码。接下来通过本文给大家介绍Android中使用ZXing生成二维码(支持添加Logo图案),需要的朋友参考下2017-01-01Andriod Studio实现保存QQ密码功能(案例代码详解)
这篇文章主要介绍了Andriod Studio实现保存QQ密码功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-03-03使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看)
这篇文章主要介绍了使用IntelliJ IDEA 配置安卓(Android)开发环境的教程详解(新手必看),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-09-09Android EditText追加空格、限制字符等方法示例
这篇文章主要给大家介绍了关于Android EditText追加空格、限制字符等的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧2019-06-06JetpackCompose Navigation导航实现流程
Navigation是Jetpack用于Android导航的组件,作用是处理页面跳转,以及页面跳转过程中的交互。使用Navigation,你就需要为每个页面设定一条唯一路径,它是一个String常量,形式是DeepLink的样子,从一个页面跳转到另一个页面,它通过输入目的地的路径进行转跳2023-01-01
最新评论