java、android生成8未随机码

	public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
		"g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
		"t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
		"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
		"W", "X", "Y", "Z" };

	/**
	 * 生成八位随机码
	 * @return
	 */
	public static String generateShortUuid() {
		StringBuffer shortBuffer = new StringBuffer();
		String uuid = UUID.randomUUID().toString().replace("-", "");
		for (int i = 0; i < 8; i++) {
			String str = uuid.substring(i * 4, i * 4 + 4);
			int x = Integer.parseInt(str, 16);
			shortBuffer.append(chars[x % 0x3E]);
		}
		return shortBuffer.toString();

	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,感谢您的提问。关于您的问题,以下是一种可能的解决方案: 1. 在布局文件中添加一个用于显示手机号码和验证码的弹窗。 在布局文件中添加以下代码: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_get_verification_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取验证码" /> <!-- 弹窗布局 --> <RelativeLayout android:id="@+id/layout_popup" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:padding="16dp" android:visibility="gone"> <TextView android:id="@+id/tv_phone_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="手机号码:" /> <TextView android:id="@+id/tv_verification_code" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_phone_number" android:text="验证码:" /> <Button android:id="@+id/btn_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="关闭" /> </RelativeLayout> </LinearLayout> ``` 2. 在 Java 代码中实现随机生成6位数号码,并在点击获取验证码按钮时显示弹窗 为按钮设置点击事件,生成6位数随机验证码并在弹窗中显示手机号码和验证码,代码如下: ``` public class MainActivity extends AppCompatActivity { private EditText etPhoneNumber; private Button btnGetVerificationCode; private RelativeLayout layoutPopup; private TextView tvPhoneNumber; private TextView tvVerificationCode; private Button btnClose; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etPhoneNumber = findViewById(R.id.et_phone_number); btnGetVerificationCode = findViewById(R.id.btn_get_verification_code); layoutPopup = findViewById(R.id.layout_popup); tvPhoneNumber = findViewById(R.id.tv_phone_number); tvVerificationCode = findViewById(R.id.tv_verification_code); btnClose = findViewById(R.id.btn_close); // 为按钮设置点击事件 btnGetVerificationCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // 获取手机号码 String phoneNumber = etPhoneNumber.getText().toString().trim(); // 生成6位数随机验证码 int verificationCode = new Random().nextInt(899999) + 100000; // 在弹窗中显示手机号码和验证码 tvPhoneNumber.setText("手机号码:" + phoneNumber); tvVerificationCode.setText("验证码:" + verificationCode); layoutPopup.setVisibility(View.VISIBLE); } }); // 为关闭按钮设置点击事件 btnClose.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { layoutPopup.setVisibility(View.GONE); } }); } } ``` 这样就可以实现点击获取验证码按钮时,显示如下弹窗,其中的手机号码为编辑框中输入的号码,验证码通过随机生成6位数号码了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值