当我像这样单击“下一步”时,我想将电话号码从 editText 放到自定义警报对话框中: 我已将 getStringExtra 放入 textView 自定义对话框,但我应该单击"is",然后显示数字成功。但我想要的是当我点击“下一步”时,显示的数字。
代码:
public void sendVerificationCode() {
phoneNumber = countryCode.getFullNumberWithPlus(); // The number i want to display to custom alert dialog
Intent moveVerification = new Intent(CreateUserActivity.this, VerificationCode.class);
moveVerification.putExtra("sendPhoneNumber", phoneNumber);
startActivity(moveVerification);
finish();
}
public void dialogVerification() {
dialogVerification = new Dialog(CreateUserActivity.this);
dialogVerification.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogVerification.setContentView(R.layout.custom_alert_dialog);
dialogVerification.setCanceledOnTouchOutside(false);
Button buttonYes = dialogVerification.findViewById(R.id.buttonYes);
Button buttonEdit = dialogVerification.findViewById(R.id.buttonEdit);
TextView displayNumber = dialogVerification.findViewById(R.id.displayNumber); // I want to display the number at here
dialogVerification.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
dialogVerification.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialogVerification.show();
}
最佳答案
- 如果您的代码在同一 Activity 中:
要么将 phoneNumber
作为参数传递给 dialogVerification()
,要么将其声明为全局变量以直接在任何函数中使用它。
将其作为参数传递:
从
sendVerificationCode()
调用dialogVerification()
:dialogverification(phoneNumber);
并替换它 -> public void dialogVerification() {
与
public void dialogVerification(int phoneNumber) {
只需将电话号码放在 textView 中
displayNumber.setText(phoneNumber.toString());
- 将其声明为全局变量:
在类声明下方的
onStart()
之前将 phoneNumber 声明为int phonNumber;
只需将电话号码放在 textView 中
displayNumber.setText(phoneNumber.toString());
- Else 如果此
dialogVerification
在另一个VerificationCode
将其传递给其他 Activity :
Intent intent = new Intent(CreateUserActivity.this, VerificationCode.class);
intent.putExtra("Phone_Number", phoneNumber);
startActivity(intent);
并在 VerificationCode
中访问该号码:
int phoneNumber= getIntent().getIntExtra("Phone_Number");
只需将电话号码放在 textView 中
displayNumber.setText(phoneNumber.toString());
PS: 是字符串,将每个int
替换成String
即可,getIntExtra
为 getStringExtra
并删除 .toString()
。
关于java - 通过 android 自定义警报对话框传递额外数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350295/