我正在创建注册布局。由于太多信息无法放入一种布局中,因此我需要使用两种
当用户选择REGISTER
按钮时,我要显示布局1,将数据保存到firebase,然后当他们选择肯定按钮时,它将关闭布局1并显示布局2。
我该怎么做?
我有两个对话框:
showVehicleDetails
AlertDialog.Builder alert_dialog = new AlertDialog.Builder(this);
alert_dialog.setTitle("VEHICLE DETAILS");
alert_dialog.setMessage("Please, fill in the following:");
LayoutInflater inflate = LayoutInflater.from(this);
View vehicle_details = inflate.inflate(R.layout.layout_vehicle_details, null);
final MaterialEditText edtYear = vehicle_details.findViewById(R.id.year);
final MaterialEditText edtCMNM = vehicle_details.findViewById(R.id.cmnm);
final MaterialEditText edtPlate = vehicle_details.findViewById(R.id.plate);
alert_dialog.setView(vehicle_details);
alert_dialog.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// check validation
if (TextUtils.isEmpty(edtYear.getText().toString())) {
Snackbar.make(rootLayout, "Please enter year of your vehicle", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtCMNM.getText().toString())) {
Snackbar.make(rootLayout, "Please enter the Color, Make and Model of your vehicle",
Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPlate.getText().toString())) {
Snackbar.make(rootLayout, "Please enter your license plate number", Snackbar
.LENGTH_SHORT).show();
return;
}
// save to firebase/ Users/Drivers ...
details = users;
details.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// get drivers id
for (DataSnapshot ss : snapshot.getChildren()) {
String id = ss.getKey();
String year = edtYear.getText().toString();
String cmnm = edtCMNM.getText().toString();
String plate = edtPlate.getText().toString();
Log.e(TAG, "year: " + year + ", cmnm: " + cmnm + ", plate: " + plate);
// data sent
details.child(id).child("year").setValue(year);
details.child(id).child("cmnm").setValue(cmnm);
details.child(id).child("plate").setValue(plate);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
AlertDialog dialog = alert_dialog.create();
dialog.show();
}
showRegisterDialog
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("REGISTER");
builder.setMessage("Please use email to register");
LayoutInflater inflater = LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.layout_register, null);
final MaterialEditText edtEmail = register_layout.findViewById(R.id.email);
final MaterialEditText edtPassword = register_layout.findViewById(R.id.password);
final MaterialEditText edtName = register_layout.findViewById(R.id.usersname);
final MaterialEditText edtPhone = register_layout.findViewById(R.id.cell);
final MaterialAnimatedSwitch policies_switch = register_layout.findViewById(R.id.policies_switch);
builder.setView(register_layout);
builder.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
dialog.dismiss();
// check validation
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter email address", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPhone.getText().toString())) {
Snackbar.make(rootLayout, "Please enter phone number", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter password", Snackbar
.LENGTH_SHORT).show();
return;
}
if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short !!!", Snackbar
.LENGTH_SHORT).show();
return;
}
// Register new user
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),
edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Save user to db
Driver driver = new Driver();
driver.setEmail(edtEmail.getText().toString());
driver.setUsersname(edtName.getText().toString());
driver.setCell(edtPhone.getText().toString());
driver.setPassword(edtPassword.getText().toString());
driver.setRates("4.5"); // TODO: Default added
// use uid to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(driver)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "Registration successful !!!",
Snackbar.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
}
});
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = builder.create();
/* If switch is on, enable REGISTER button */
policies_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked) {
if (isChecked) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); // if checked, enable
}
}
});
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); // set disabled by default
}
按现在的样子,当我单击Register
按钮时,它将显示第二个布局showRegisterDialog。
我希望它首先显示showVehicleDetails
,并且当我选择肯定按钮SUBMIT
时它应该显示showRegisterDetails
运行代码时,请在onCreate
中:
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showVehicleDetails();
showRegisterDialog();
}
});
这是我按下“注册”按钮时发生的情况,它停留在此位置而不是关闭
答案 0 :(得分:1)
好吧,我不建议这样做,因为我更喜欢@Tamir在他的回答中表示的内容,但是这是您需要做的,因此以后可以重构以获取更干净,更简单的代码以帮助您使用分开并轻松调试。
正如我在评论中所述,这是您将要更改的修改 关于showVehicleDetails:
AlertDialog.Builder alert_dialog = new AlertDialog.Builder(this);
alert_dialog.setTitle("VEHICLE DETAILS");
alert_dialog.setMessage("Please, fill in the following:");
LayoutInflater inflate = LayoutInflater.from(this);
View vehicle_details = inflate.inflate(R.layout.layout_vehicle_details, null);
final MaterialEditText edtYear = vehicle_details.findViewById(R.id.year);
final MaterialEditText edtCMNM = vehicle_details.findViewById(R.id.cmnm);
final MaterialEditText edtPlate = vehicle_details.findViewById(R.id.plate);
alert_dialog.setView(vehicle_details);
alert_dialog.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
// check validation
if (TextUtils.isEmpty(edtYear.getText().toString())) {
Snackbar.make(rootLayout, "Please enter year of your vehicle", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtCMNM.getText().toString())) {
Snackbar.make(rootLayout, "Please enter the Color, Make and Model of your vehicle",
Snackbar.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPlate.getText().toString())) {
Snackbar.make(rootLayout, "Please enter your license plate number", Snackbar
.LENGTH_SHORT).show();
return;
}
// save to firebase/ Users/Drivers ...
details = users;
details.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// get drivers id
for (DataSnapshot ss : snapshot.getChildren()) {
String id = ss.getKey();
String year = edtYear.getText().toString();
String cmnm = edtCMNM.getText().toString();
String plate = edtPlate.getText().toString();
Log.e(TAG, "year: " + year + ", cmnm: " + cmnm + ", plate: " + plate);
// data sent
details.child(id).child("year").setValue(year);
details.child(id).child("cmnm").setValue(cmnm);
details.child(id).child("plate").setValue(plate);
}
showRegisterDialog();
dialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
AlertDialog dialog = alert_dialog.create();
dialog.show();}
这是您将要修改的修改 关于showRegisterDialogue:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("REGISTER");
builder.setMessage("Please use email to register");
LayoutInflater inflater = LayoutInflater.from(this);
View register_layout = inflater.inflate(R.layout.layout_register, null);
final MaterialEditText edtEmail = register_layout.findViewById(R.id.email);
final MaterialEditText edtPassword = register_layout.findViewById(R.id.password);
final MaterialEditText edtName = register_layout.findViewById(R.id.usersname);
final MaterialEditText edtPhone = register_layout.findViewById(R.id.cell);
final MaterialAnimatedSwitch policies_switch = register_layout.findViewById(R.id.policies_switch);
builder.setView(register_layout);
builder.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
// check validation
if (TextUtils.isEmpty(edtEmail.getText().toString())) {
Snackbar.make(rootLayout, "Please enter email address", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPhone.getText().toString())) {
Snackbar.make(rootLayout, "Please enter phone number", Snackbar
.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(edtPassword.getText().toString())) {
Snackbar.make(rootLayout, "Please enter password", Snackbar
.LENGTH_SHORT).show();
return;
}
if (edtPassword.getText().toString().length() < 6) {
Snackbar.make(rootLayout, "Password too short !!!", Snackbar
.LENGTH_SHORT).show();
return;
}
// Register new user
auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),
edtPassword.getText().toString())
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Save user to db
Driver driver = new Driver();
driver.setEmail(edtEmail.getText().toString());
driver.setUsersname(edtName.getText().toString());
driver.setCell(edtPhone.getText().toString());
driver.setPassword(edtPassword.getText().toString());
driver.setRates("4.5"); // TODO: Default added
// use uid to key
users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(driver)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Snackbar.make(rootLayout, "Registration successful !!!",
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Snackbar.make(rootLayout, "Failed" + e.getMessage(),
Snackbar.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog dialog = builder.create();
/* If switch is on, enable REGISTER button */
policies_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(boolean isChecked) {
if (isChecked) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true); // if checked, enable
}
}
});
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); // set disabled by default
}
对于ClickListener,您将只删除showRegisterDialogue,就像这样
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showVehicleDetails();
}
});
答案 1 :(得分:0)
为此,我将使用2个自定义对话框类。
每个对话框都有他的班级和布局,现在我们需要这样做:
显示对话框A
向Firebase发送一些数据并等待良好的回调
从对话框A获得回调后,关闭它并显示第二个对话框。
听起来不错?让我们看看我们如何写这个:
这将是您的对话框类(或类似的东西,仅是示例):
public class FullSizeImageDialog extends Dialog {
private ImageView imageView;
private ProgressBar fullImageProgreesBar;
private Context dialogContext;
public FullSizeImageDialog(@NonNull Context context) {
super(context);
setContentView(R.layout.full_size_image_dialog);
dialogContext = context;
imageView = findViewById(R.id.full_size_image);
fullImageProgreesBar = findViewById(R.id.fullImageProgreesBar);
}
}
这是对话框的布局(在我的情况下为R.id.full_size_image
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Place your views here-->
</android.support.constraint.ConstraintLayout>
当您想显示对话框时,这非常简单:
FullSizeImageDialog dialog = new FullSizeImageDialog ();
dialog.show();
当第一个对话框从Firebase调用回调时,对话框A的dismiss()
和对话框B的show()
会得到调用。
为什么我建议使用此而不是AlertDialog.Builder
?
您可以使用自定义对话框更快地构建布局。
在拥有自定义布局时,无需编写大量代码即可添加视图。
让您更容易(或者我相信)在一个方法中看到myCoolDialog.show();
,而不是50行或更多的代码。
您是否需要更改有关对话框外观和代码的任何内容?很好,请转到单独的类并进行更改,而不是在活动中添加20条代码行。