Java 通过android自定义警报对话框传递额外数据
当我单击“下一步”时,我想将电话号码从editText放入自定义警报对话框,如下所示: 我已将getStringExtra置于textView自定义对话框中,但我应该单击“是”,然后显示数字成功。但我想要的是当我点击“下一步”时,数字就会显示出来 代码:Java 通过android自定义警报对话框传递额外数据,java,android,Java,Android,当我单击“下一步”时,我想将电话号码从editText放入自定义警报对话框,如下所示: 我已将getStringExtra置于textView自定义对话框中,但我应该单击“是”,然后显示数字成功。但我想要的是当我点击“下一步”时,数字就会显示出来 代码: public void sendVerificationCode() { phoneNumber = countryCode.getFullNumberWithPlus(); // The number i want to displa
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();
}
您可以创建一个静态类,并创建一个静态字段,以保存您的电话号码变量值。然后,您可以在任何地方访问phoneNumber变量。 这不是一个最佳实践,但这种方法总是有效的
- 如果您的代码处于同一活动中:
phoneNumber
作为参数传递给dialogVerification()
或将其声明为全局变量,以便在任何函数中直接使用
sendVerificationCode()
调用dialogVerification()
:
对话框验证(电话号码)代码>
public void dialogVerification(){
与
公共无效对话框验证(int phoneNumber){
只需在文本视图中输入电话号码即可
displayNumber.setText(phoneNumber.toString());
onStart()
前面的电话号码声明为int phonNumber;
displayNumber.setText(phoneNumber.toString());
- Else如果此
位于另一个对话框Verification
VerificationCode
Intent intent = new Intent(CreateUserActivity.this, VerificationCode.class);
intent.putExtra("Phone_Number", phoneNumber);
startActivity(intent);
并在VerificationCode
中访问该号码,如下所示:
int phoneNumber= getIntent().getIntExtra("Phone_Number");
只需在文本视图中输入电话号码即可
displayNumber.setText(phoneNumber.toString());
PS:它是一个字符串,只需将每个int
替换为string
,getIntExtra
替换为getStringExtra
,然后删除.toString()
}请分享您的代码我是在@KishanMauryaI编辑的在您的代码中没有看到调用
对话框验证
函数。如果调用函数对话框验证
来显示对话框,只需将电话号码
发送到类似对话框验证(字符串电话号码)的函数
然后设置文本视图值,如displayNumber.setText(电话号码)
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();
}