无法从警报对话框中获取文本 Android

Cannot getText From Alert Dialog Android

我在 EditText 的 getText 中遇到了问题,我已经使用了我知道的所有方法,但它不起作用尽管我填写了 editText,但答案总是说字段是必需的。这是我的代码

 private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();

    Button mLogin = (Button) mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AllowingToUpdate();
        }
    });
}

private void AllowingToUpdate() {
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);

    //I want to get Text From mMesin
    EditText mMesin = (EditText) mView.findViewById(R.id.etMesin);
    String NoMesin = mMesin.getText().toString();

    //Toast display always "Field is required" eventhough i have filled the editText

    if(mMesin.getText().toString().equals(""))
    {
        Toast.makeText(this,"Field Is Required....",Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(this,"This Is Work....."+NoMesin,Toast.LENGTH_SHORT).show();
    }
}

您正在再次膨胀布局,这将为您提供一个未附加到 AlertDialog 的新布局(视图)。 保持简单并使用添加到 AlertDialog 的相同视图。看下面的代码你就会明白了。

 private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog_activation, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();
    final EditText mMesin = (EditText) mView.findViewById(R.id.etMesin);
    Button mLogin = (Button) mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(mMesin.getText().toString())) {
                Toast.makeText(ActivityName.this, "Field Is Required....", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(ActivityName.this, "This Is Work....." + mMesin.getText().toString().trim(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

您可以将mView作为参数传递给方法AllowingToUpdate。这也将解决它。

使用以下内容更改您的 buildDialog 方法:

使您的 String NoMesin 全局化。

private void buildDialog(int animationSource) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    View mView = getLayoutInflater().inflate(R.layout.dialog, null);
    builder.setView(mView);
    AlertDialog dialog = builder.create();
    dialog.getWindow().getAttributes().windowAnimations = animationSource;
    dialog.show();

    final EditText mMesin = mView.findViewById(R.id.etMesin);

    mMesin.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            NoMesin = mMesin.getText().toString();
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            NoMesin = mMesin.getText().toString();
        }

        @Override
        public void afterTextChanged(Editable editable) {
            NoMesin = mMesin.getText().toString();
        }
    });

    //Toast display always "Field is required" eventhough i have filled the editText

    Button mLogin = mView.findViewById(R.id.btnAktivasi);
    mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (TextUtils.isEmpty(NoMesin)) {
                Toast.makeText(MainActivity.this, "Field Is Required....", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "This Is Work....." + NoMesin, Toast.LENGTH_SHORT).show();
            }
        }
    });
}