java - 通过 android 自定义警报对话框传递额外数据

标签 java android

当我像这样单击“下一步”时,我想将电话号码从 editText 放到自定义警报对话框中: 1 我已将 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(),要么将其声明为全局变量以直接在任何函数中使用它。

  1. 将其作为参数传递:

    sendVerificationCode() 调用 dialogVerification() :

    dialogverification(phoneNumber);

并替换它 -> public void dialogVerification() {

public void dialogVerification(int phoneNumber) {

只需将电话号码放在 textView 中

displayNumber.setText(phoneNumber.toString());

  1. 将其声明为全局变量: 在类声明下方的 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/

相关文章:

java - 在 Eclipse 中运行我签名的发布 keystore 时出现问题

android - 返回不区分大小写的 sqlite 数据库

android - 实现错误-picasso:2.71828(发现多个文件具有与OS无关的路径)

java - 使用 iText 在网络浏览器中显示 PDF 文件

java - 创建一个热点并将没有背景的图像(如地球图标)设置到我的面板

java - findLibrary 在 Android Studio 中使用 OpenCV4Android 返回 null

javascript - Android 浏览器中某些键没有按键事件

javascript - android中用户界面的webview

java - ActiveMQ - 队列中主题和并发消费者之间的区别?

java - 如何使用 ZK 在 UI 中迭代和加载属性文件值?