Android生成隨機(jī)數(shù)的方法實(shí)例
更新時(shí)間:2021年03月22日 11:13:02 作者:你先去買飯
這篇文章主要為大家詳細(xì)介紹了Android生成隨機(jī)數(shù)的方法實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Android生成隨機(jī)數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
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>
效果圖片
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 如何用java生成指定范圍的隨機(jī)數(shù)
- Java生成隨機(jī)數(shù)的2種示例方法代碼
- java生成字母數(shù)字組合的隨機(jī)數(shù)示例 java生成隨機(jī)數(shù)
- 史上最全的java隨機(jī)數(shù)生成算法分享
- java生成指定范圍隨機(jī)數(shù)的多種代碼
- Java實(shí)現(xiàn)按權(quán)重隨機(jī)數(shù)
- Java中生成隨機(jī)數(shù)的實(shí)現(xiàn)方法總結(jié)
- JAVA獲得包含0-9、a-z、A-Z范圍內(nèi)字符串的的隨機(jī)數(shù)實(shí)例
- Java獲取隨機(jī)數(shù)的3種方法
- Java中的隨機(jī)數(shù)詳解
相關(guān)文章
Android開發(fā)之Sqliteopenhelper用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之Sqliteopenhelper用法,實(shí)例分析了SQLiteOpenHelper類操作數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2015-05-05Android UI 實(shí)現(xiàn)老虎機(jī)詳解及實(shí)例代碼
這篇文章主要介紹了Android UI 實(shí)現(xiàn)老虎機(jī)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Qt5.12.6配置Android Arm開發(fā)環(huán)境(圖文)
本文主要介紹了Qt5.12.6配置Android Arm開發(fā)環(huán)境,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Android幀動(dòng)畫、補(bǔ)間動(dòng)畫、屬性動(dòng)畫用法詳解
安卓的三種動(dòng)畫,幀動(dòng)畫,補(bǔ)間動(dòng)畫,屬性動(dòng)畫,大家了解多少,知道如何使用嗎?本文就為大家簡單介紹Android幀動(dòng)畫、補(bǔ)間動(dòng)畫、屬性動(dòng)畫的使用方法,需要的朋友可以參考下2016-11-11