使用Android点击按钮产生随机数

关于使用Android点击后产生随机数

案例如下:

package com.qiang.sifter;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.qiang.But.MyUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private  Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn =(Button)findViewById(R.id.btnse);
        btn.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View view) {
    btn.setText(""+MyUtils.getRand(1,6));
    }
}

在 btn.setText(MyUtils.getRand(1,6));出现问题的要进行类型转换

  • 方法如下:TextView.setText("" + arg) btn.setText(""+MyUtils.getRand(1,6));
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <Button
            android:layout_width="wrap_content"
            android:id="@+id/btnse"
            android:layout_height="wrap_content"
            android:text="掷色子"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.qiang.But;

public class MyUtils {
    public static int getRand(int a, int b) {
        return (int) (Math.random() * (b - a + 1) + a);
    }
}

在这里插入图片描述

欢迎大家交流沟通

可以使用 Service 在后台生成随机数。以下是一份示例代码: ```java public class RandomNumberService extends Service { private static final String TAG = "RandomNumberService"; private final IBinder mBinder = new LocalBinder(); private final Random mGenerator = new Random(); @Override public void onCreate() { Log.d(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand"); return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); return mBinder; } public class LocalBinder extends Binder { RandomNumberService getService() { return RandomNumberService.this; } } public int getRandomNumber() { return mGenerator.nextInt(); } } ``` 然后在你的 Activity 中,通过启动 Service 和绑定 Service 的方式获取随机数: ```java public class MainActivity extends AppCompatActivity { private boolean mBound = false; private RandomNumberService mService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, RandomNumberService.class); startService(intent); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); if (mBound) { unbindService(mConnection); mBound = false; } } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { RandomNumberService.LocalBinder binder = (RandomNumberService.LocalBinder) iBinder; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName componentName) { mBound = false; } }; public void onGetRandomNumber(View view) { if (mBound) { int randomNumber = mService.getRandomNumber(); Toast.makeText(this, "Random number: " + randomNumber, Toast.LENGTH_SHORT).show(); } } } ``` 其中 `onGetRandomNumber` 是一个按钮点击事件,当用户点击按钮时,会调用 `getRandomNumber` 方法获取随机数,并通过 Toast 显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jcrry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值