【发布时间】:2018-10-04 11:56:06
【问题描述】:
我正在尝试验证 AlertDialog.Builder 中的 TextInputLayout。请帮助我。我尝试以一种简单的方式对其进行验证,但是当按下肯定按钮时,什么也没有发生。
谢谢
private TextInputLayout txtUpdateWeight;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.MyDialogTheme);
final FirebaseDatabase db = FirebaseDatabase.getInstance();
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_update_weight_dialog, null);
txtUpdateWeight = view.findViewById(R.id.tlUpdateWeight);
builder.setView(view)
.setTitle("Update Weight")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (!validateField()) {
return;
}
String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();
DatabaseReference myDB = db.getReference().child("Additional details");
Map<String, Object> updates = new HashMap<>();
updates.put("weight", etWeight);
myDB.updateChildren(updates);
Toast.makeText(getContext(), "Weight updated Successfully", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
这是我的验证函数
public boolean validateField() {
String etWeight = txtUpdateWeight.getEditText().getText().toString().trim();
if (etWeight.isEmpty()) {
txtUpdateWeight.setError("Enter your weight");
return false;
} else {
txtUpdateWeight.setError(null);
return true;
}
}
【问题讨论】:
-
你调试了吗??
-
您好,经过深入研究,我在该链接上找到了答案。 stackoverflow.com/questions/11363209/…感谢帮助
标签: android firebase firebase-realtime-database