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>
效果圖片
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考2. 關(guān)于PHP程序員解決問題的能力3. intellij idea使用git stash暫存一次提交的操作4. 解決idea web項(xiàng)目中out目錄更新不同步問題5. 關(guān)于Spring自定義XML schema 擴(kuò)展的問題(Spring面試高頻題)6. Spring Bean管理注解方式代碼實(shí)例7. 淺談django不使用restframework自定義接口與使用的區(qū)別8. Jsp servlet驗(yàn)證碼工具類分享9. ASP.NET MVC實(shí)現(xiàn)區(qū)域或城市選擇10. 一個(gè)用xslt樣式將xml解析為xhtml的類TransformBinder(兼容FF和IE7.0)
