Android生成隨機(jī)數(shù)的方法實(shí)例
本文實(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: android:background='#ffffff' android:layout_width='wrap_content' android:layout_height='wrap_content'></Button> <EditText android: android:hint='0-n' android:layout_width='match_parent' android:layout_height='wrap_content'></EditText> <TextView android:textColor='#DD0D0D' android:textSize='50dp' android: android:text='0' android:layout_width='wrap_content' android:layout_height='wrap_content'></TextView></LinearLayout>
效果圖片
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. React+umi+typeScript創(chuàng)建項(xiàng)目的過(guò)程2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析4. SharePoint Server 2019新特性介紹5. .Net core 的熱插拔機(jī)制的深入探索及卸載問(wèn)題求救指南6. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問(wèn)題7. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題8. ASP編碼必備的8條原則9. 無(wú)線(xiàn)標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)10. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp
