android - 在 AlertDialog 中验证 EditText

标签 android firebase firebase-realtime-database

我正在尝试验证 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;
    }
}

最佳答案

你应该使用 TextUtils

  if(TextUtils.isEmpty(etWeight))

注意

您应该使用 TextInputEditText 而不是 TextInputLayout

我假设你的 XML 是

<android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           >

            <android.support.design.widget.TextInputEditText
                android:id="@+id/your_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </android.support.design.widget.TextInputLayout>

然后

 String etWeight = textInputEditTextOBJ.getText().toString().trim();

关于android - 在 AlertDialog 中验证 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646075/

相关文章:

java - 无法启动 Activity ComponentInfo : Error inflating class android. support.design.widget.CoordinatorLayout

android - TravisCI,使用Gradle构建并使用Crashlytics进行部署

android - 工具栏中的 Material "close"按钮而不是返回

android - GSON 在从共享 prefs 获取时将之前存储的 List 转换为 LinkedTreeMaps 的 ArrayList

android - Firebase:数据库 '/google_app_measurement_local.db' 未打开

ios - setValue 方法未更新到 Firebase 数据库

ios - Firebase 在嵌套节点中按值查询

android - Android GPS 记录服务的电池耗尽问题

Firebase 托管构建失败,因为 process.env.ci 设置为 true

javascript - 在聚合物模块中使用观察器