简体   繁体   中英

closing dialog on clicking outside

What I want is a dialog without any button, and closing that dialog when I click outside the dialog body. Is it possible?

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        openCredit();
    }

   //THIS IS ONE CUSTOM DIALOG
    public void openCredit(){ 
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.activity_splash, null));
        builder.show();
    }
}

EDIT: I have search and found two functions setCanceledOnTouchOutside() and setCancelable(). First one is not working with my method of AlertDialog, giving error of "The method setCanceledOnTouchOutside(boolean) is undefined for the type AlertDialog.Builder". And 2nd one is just for pressing back button.

So, I change my code like below and it is now working. Thanks.

public void openCredit(){
        AlertDialog builder = new AlertDialog.Builder(this).create();
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.activity_splash, null));
        builder.setCancelable(true);
        builder.show();
        builder.setCanceledOnTouchOutside(true);
    }

你尝试过这个吗..

builder.setCancelable(true);

为此添加此行

builder.setCancelable(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM