亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Android 自定義警報對話框傳遞額外數據

通過 Android 自定義警報對話框傳遞額外數據

江戶川亂折騰 2023-10-13 16:22:57
當我單擊“下一步”時,我想將電話號碼從 editText 放入自定義警報對話框,如下所示:我已將 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();}
查看完整描述

4 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

  • 如果您的代碼位于同一活動中:

phoneNumber作為參數傳遞給dialogVerification()或將其聲明為全局變量以直接在任何函數中使用它。

  1. 要將其作為參數傳遞:

    致電dialogVerification()來自sendVerificationCode()

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

  • 否則如果這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 替換intStringgetIntExtraasgetStringExtra并刪除該.toString()。


查看完整回答
反對 回復 2023-10-13
?
楊__羊羊

TA貢獻1943條經驗 獲得超7個贊

public class MainActivity extends AppCompatActivity {

EditText phonenum;

Button next;



@Override

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


}

}


查看完整回答
反對 回復 2023-10-13
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

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

? ? ? ? }

? ? });

}


查看完整回答
反對 回復 2023-10-13
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

您可以創建一個靜態類并創建一個靜態字段來保存phoneNumber變量值。然后您可以在任何地方訪問您的phoneNumber 變量。不是最佳實踐,但這種方法總能奏效



查看完整回答
反對 回復 2023-10-13
  • 4 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號