关闭 AlertDialog.Builder
Dismiss AlertDialog.Builder
在我的代码中,我正在尝试进行 AlertDialog 登录,但按下按钮后我无法将其关闭,有人知道吗?我尝试从构建器创建一个 AlertDialog 对象,但它不起作用。这是我的代码:
public class MainActivity extends AppCompatActivity {
ImageButton login;
Button signin;
EditText user, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (ImageButton)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
createSigninDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
});
}
public AlertDialog createSigninDialog (){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(view);
signin = (Button)view.findViewById(R.id.button_ingresar);
user = (EditText)view.findViewById(R.id.user_input);
password = (EditText)view.findViewById(R.id.password_input);
signin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
//Dismiss here
} else {
Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
//Dismiss here
}
}
}
);
return builder.show();
}
}
您应该调用 builder.create()
而不是调用 builder.show()
。它 returns 一个对话实例给你,然后 show()
和 dismiss()
方法将可以调用。
您的函数应如下所示:
public AlertDialog createSigninDialog (){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(view);
final AlertDialog dialog = builder.create();
signin = (Button)view.findViewById(R.id.singing);
user = (EditText)view.findViewById(R.id.user_input);
password = (EditText)view.findViewById(R.id.password_input);
signin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
}
}
}
);
return dialog;
}
然后你可以这样调用它:
createSigninDialog().show();
在我的代码中,我正在尝试进行 AlertDialog 登录,但按下按钮后我无法将其关闭,有人知道吗?我尝试从构建器创建一个 AlertDialog 对象,但它不起作用。这是我的代码:
public class MainActivity extends AppCompatActivity {
ImageButton login;
Button signin;
EditText user, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (ImageButton)findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
createSigninDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
});
}
public AlertDialog createSigninDialog (){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(view);
signin = (Button)view.findViewById(R.id.button_ingresar);
user = (EditText)view.findViewById(R.id.user_input);
password = (EditText)view.findViewById(R.id.password_input);
signin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.imagen_click));
if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
//Dismiss here
} else {
Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
//Dismiss here
}
}
}
);
return builder.show();
}
}
您应该调用 builder.create()
而不是调用 builder.show()
。它 returns 一个对话实例给你,然后 show()
和 dismiss()
方法将可以调用。
您的函数应如下所示:
public AlertDialog createSigninDialog (){
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(view);
final AlertDialog dialog = builder.create();
signin = (Button)view.findViewById(R.id.singing);
user = (EditText)view.findViewById(R.id.user_input);
password = (EditText)view.findViewById(R.id.password_input);
signin.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (user.getText().toString().equals("1234567890") && password.getText().toString().equals("1234")){
Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
} else {
Toast.makeText(MainActivity.this, "Datos incorrectos", Toast.LENGTH_SHORT).show();
//Dismiss here
dialog.dismiss();
}
}
}
);
return dialog;
}
然后你可以这样调用它:
createSigninDialog().show();