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

当我单击“下一步”时,我想将电话号码从 editText 放入自定义警报对话框,如下所示:

https://img1.sycdn.imooc.com/6528fe9b00014a6313660767.jpg

我已将 getStringExtra 放入 textView 自定义对话框,但我应该单击“是”,然后显示号码成功。但我想要的是当我单击“下一步”时,显示数字。


代码 :


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();

}


江户川乱折腾
浏览 131回答 4
4回答

慕无忌1623718

如果您的代码位于同一活动中:将phoneNumber作为参数传递给dialogVerification()或将其声明为全局变量以直接在任何函数中使用它。要将其作为参数传递:致电dialogVerification()来自sendVerificationCode():dialogverification(phoneNumber);并替换这个->public void dialogVerification() {和public void dialogVerification(int phoneNumber) {只需将电话号码放入 textView 中即可displayNumber.setText(phoneNumber.toString());将其声明为全局变量:在onStart()类声明的正下方之前将phoneNumber声明为int phonNumber;只需将电话号码放入 textView 中即可displayNumber.setText(phoneNumber.toString());否则如果这dialogVerification是在另一个VerificationCode将其传递给其他活动: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:它是一个字符串,只需将 every 替换int为String, getIntExtraasgetStringExtra并删除该.toString()。

杨__羊羊

public class MainActivity extends AppCompatActivity {EditText phonenum;Button next;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    phonenum = findViewById(R.id.editText);    next = findViewById(R.id.button);    next.setOnClickListener(new View.OnClickListener() {        public void onClick(View v) {            // Perform action on click            go();        }    });}void go(){    String somenumber = String.valueOf(phonenum.getText());    AlertDialog dialog;    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);    alertDialog.setTitle("Number verification");    alertDialog.setMessage(somenumber + "\n" + "is your number above correct ?");    alertDialog.setCancelable(false);    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {        public void onClick(DialogInterface dialog, int which) {            //call function to start next activity        }    });    alertDialog.setNegativeButton("Edit", new DialogInterface.OnClickListener() {        @Override        public void onClick(DialogInterface dialog, int which) {            finish();        }    });    dialog = alertDialog.create();    dialog.show();}}

慕盖茨4494581

我在android最新版本中使用View Binding。我自定义了警报对话框并将所需的值传递给对话框。private ConnectDialogBinding connectDialogBinding;private String chargerID;private void connectDialog() {    // Create the object of    // AlertDialog Builder class    AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);    connectDialogBinding = ConnectDialogBinding.inflate(getLayoutInflater());    builder.setView(connectDialogBinding.getRoot());    connectDialogBinding.txtID.setText(chargerID);    builder.setCancelable(false);    // Create the Alert dialog    AlertDialog alertDialog = builder.create();    // Show the Alert Dialog box    alertDialog.show();    connectDialogBinding.cancelBtn.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            alertDialog.cancel();        }    });}

斯蒂芬大帝

您可以创建一个静态类并创建一个静态字段来保存phoneNumber变量值。然后您可以在任何地方访问您的phoneNumber 变量。不是最佳实践,但这种方法总能奏效
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java