用Android studio代码实现点击获取验证码按钮时,显示如下弹窗,其中的手机号码为编辑框中输入的号码,验证码通过随机生成6位数号码
时间: 2023-06-11 11:04:51 浏览: 59
您好,感谢您的提问。关于您的问题,以下是一种可能的解决方案:
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位数号码了。