看代码:
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View view1 = inflater.inflate(R.layout.alert, (ViewGroup) findViewById(R.id.alertdialog));
builder.setView(view1);
final AlertDialog dialog = builder.show();
view1.findViewById(R.id.btn_invent).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("tag", "onClick: "+"点击取消" );
dialog.dismiss();
}
}); }
});
}
} 要取消dialog先要获取dialog,再dismiss(),直接dismiss()是没有用的;
demo下载地址:https://github.com/renjiemei1225/AlertDialogTest2