我在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();
}
}
答案 0 :(得分:0)
您正在再次扩大版式,这将为您提供一个新的版式(视图),该版式未附加到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
。这也将解决它。
答案 1 :(得分:0)
使用以下更改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();
}
}
});
}