質問

I have an alert dialog which is invoked when the user click back button on device but this alert is too short and the user cant read anything inside it or do any thing with it..

AlertDialog alertDialog = new AlertDialog.Builder(birthDate.this).create();
                    // Setting Dialog Title
                    alertDialog.setTitle("Alert Dialog");
                    // Setting Dialog Message
                    alertDialog.setMessage("Welcome to AndroidHive.info");
                    // Setting OK Button
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    // Write your code here to execute after dialog closed
                    Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_LONG).show();
                    }

I want to make the period of this alert dialog longer

役に立ちましたか?

解決

I think you are calling alertdialog and finish() both are at same time. Try to finish the activity in the alert dialog as

alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog closed
                Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_LONG).show();
                finish();// here calling finish if user click ok button.
                }

他のヒント

My answer refer to your point I want to make the period of this alert dialog longer

long time=System.currentTimeMillis();
Show Dialog

When dismissing dialog then again long time2=System.currentTimeMillis(); So time2-time1 is your required time

Updated

You are saying AlertDialog dismiss on back press button of device then make AlertDialog cancel-able false.

I am assuming that here you are asking about the Toast which is getting displayed on click of alert dialog button. You can do that by increasing the duration of Toast to be appeared. Just replace Toast.LENGTH_LONG by whatever duration(in milliseconds) you want.

Ex.

Toast.makeText(getApplicationContext(), "You clicked on OK", 5000).show();

Try below code. This will give you enough time to show your Alert dialog. I've give 3 seconds by giving 3000 as the parameter, you can change according to your requirement.

  @Override
public void onBackPressed() {
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.setTitle("Title of your message!");
    alertDialog.setMessage("Your message to user");
    alertDialog.show();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.super.onBackPressed();
        }
    }, 3000);

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top