• 安卓开发实例:随机数


    点击按钮生成一个1-100之间的随机数
    在这里插入图片描述

    activity_random_number.xml

    
    <androidx.constraintlayout.widget.ConstraintLayout
      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=".RandomNumber">
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        <TextView
          android:text="-"
          android:textSize="200sp"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:id="@+id/tv_random_number"
          tools:ignore="HardcodedText,MissingConstraints"/>
        <Button
          android:text="随机数"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/btn_generate_random_number"
          tools:ignore="HardcodedText"/>
        <TextView
          android:text="点击按钮生成随机数"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:id="@+id/tv1"
          tools:ignore="HardcodedText"/>
      LinearLayout>
    androidx.constraintlayout.widget.ConstraintLayout>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    RandomNumber.java

    package com.weijun901.show;
    
    import android.view.View;
    import android.widget.TextView;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class RandomNumber extends AppCompatActivity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random_number);
    
        // 设置标题栏的文字
        getSupportActionBar().setTitle("随机数");
    
        TextView tvRandomNumber = findViewById(R.id.tv_random_number);
        View btnGenerateRandomNumber = findViewById(R.id.btn_generate_random_number);
    
        btnGenerateRandomNumber.setOnClickListener(view -> {
          int randomNumber = (int) (Math.random() * 100);
          tvRandomNumber.setText(String.valueOf(randomNumber));
        });
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    进阶JAVA篇-如何理解作为参数使用的匿名内部类与 Arrays 类的常用API(九)
    高并发场景QPS等专业指标揭秘大全与调优实战
    Opencv | 直方图
    解决方案不显示分类的
    数字文档管理对您的业务是否具有成本效益?
    IEEE COMMUNICATIONS LETTERS (ICL) 投稿状态记录
    1111 修复公路
    采用BERT-BiLSTM-CRF模型的中文位置语义解析
    Postman:API测试之Postman使用完全指南
    网络攻防备课笔记
  • 原文地址:https://blog.csdn.net/weixin_45853406/article/details/134079090