Programming

외부 활동시 Android 활동 대화 상자가 닫히지 않도록 방지

procodes 2020. 5. 6. 22:20
반응형

외부 활동시 Android 활동 대화 상자가 닫히지 않도록 방지


Theme.Dialog 스타일을 사용하는 활동이있어 다른 활동 위에 떠있는 창입니다. 그러나 백그라운드 활동에서 대화 상자 창 외부를 클릭하면 대화 상자가 닫힙니다. 이 행동을 어떻게 막을 수 있습니까?


이것은 당신을 도울 수 있습니다. 외부 이벤트를 처리하는 방법입니다.

창 밖에서 터치했을 때 활동과 같은 대화 상자를 취소하는 방법은 무엇입니까?

이벤트를 잡아서 아무것도하지 않으면 폐막을 막을 수 있다고 생각합니다. 그러나 이상한 생각이 무엇인지, 활동 대화 상자의 기본 동작을해야한다는 것입니다 하지 당신이 외부 터치하면 자체를 닫습니다.

(PS : 코드는 WindowManager.LayoutParams를 사용합니다)


뒤로 키를 눌렀을 때 대화 상자가 사라지는 것을 방지하려면 이것을 사용하십시오

dialog.setCancelable(false);

외부 터치에서 대화 상자가 사라지는 것을 방지하려면 이것을 사용하십시오.

 dialog.setCanceledOnTouchOutside(false);

실제로 가지고있는 것은 활동 (대화 상자처럼 보이더라도)이므로 setFinishOnTouchOutside(false)배경 활동을 클릭 할 때 활동을 유지하려면 활동에서 전화해야합니다 .

편집 : 이것은 안드로이드 API 레벨 11 이상에서만 작동합니다


나를 위해 일한 DialogFragment것은 취소 할 수없는 세트 를 만드는 것이 었습니다 .

dialog.setCancelable(false);

API 10이 높은 경우 외부를 터치하면 대화 상자가 사라지는 반면 API 11보다 낮은 경우 대화 상자가 사라지지 않습니다. 이를 방지하려면 다음을 수행해야합니다.

에서 styles.xml:<item name="android:windowCloseOnTouchOutside">false</item>

또는

에서 onCreate()방법을 사용합니다 :this.setFinishOnTouchOutside(false);

참고 : API 10 이하의 경우이 방법은 적용되지 않으며 필요하지 않습니다.


onCreate에서 대화 상자를 활동으로 사용할 때 이것을 추가하십시오.

setFinishOnTouchOutside(false);

이 코드를 사용하십시오.

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
 alertDialog.setCancelable(false);


대화 상자를 취소 가능으로 설정하면 충분하며 경고 대화 상자 외부를 터치하거나 뒤로 단추를 클릭하면 경고 대화 상자가 사라집니다. 따라서 이것을 사용하십시오 :

setCancelable(false)

그리고 다른 기능은 더 이상 필요하지 않습니다. dialog.setCanceledOnTouchOutside(false);

임시 대화 상자를 작성하고이 코드 행을 작성해야하는 경우 다음 예를 참조하십시오.

new AlertDialog.Builder(this)
                        .setTitle("Trial Version")
                        .setCancelable(false)
                        .setMessage("You are using trial version!")
                        .setIcon(R.drawable.time_left)
                        .setPositiveButton(android.R.string.yes, null).show();

간단히,

alertDialog.setCancelable(false);

사용자가 대화 상자 외부를 클릭하지 못하게합니다.


Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true); 
//use this to dismiss the dialog on outside click of dialog

dialog.setCanceledOnTouchOutside(false);
//use this for not to dismiss the dialog on outside click of dialog.

대화에 대한 자세한 내용은이 링크를 참조하십시오 .

dialog.setCancelable(false);
//used to prevent the dismiss of dialog on backpress of that activity

dialog.setCancelable(true);
//used to dismiss the dialog on onbackpressed of that activity

setFinishOnTouchOutside(false)API> 11에 사용 하고 활동 테마 대화 상자 인 Android의 기본 동작이 API <11 : 외부 터치에서 끝나지 않기 때문에 걱정하지 마십시오.


나는 이것을 onCreate ()에서 사용하며 모든 버전의 Android에서 작동하는 것 같습니다. 5.0 및 4.4.x에서 테스트되었지만 Gingerbread에서는 테스트 할 수 없습니다 .GB를 실행하는 Samsung 장치는 기본적으로 다음과 같은 방법으로 사용됩니다.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        setFinishOnTouchOutside(false);
    }
    else
    {
        getWindow().clearFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    }

    super.onCreate(savedInstanceState);

        alert.setCancelable(false);
        alert.setCanceledOnTouchOutside(false);

I guess this will help you.It Worked For me


Here is my solution:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setCancelable(false);

Also is possible to assign different action implementing onCancelListener:

alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){                   
    @Override
    public void onCancel(DialogInterface dialogInterface) {
        //Your custom logic
    } 
});

I was facing the same problem. To handle it I set a OntouchListener to the dialog and do nothing inside. But Dialog dismiss when rotating screen too. To fix it I set a variable to tell me if the dialog has normally dismissed. Then I set a OnDismissListener to my dialog and inside I check the variable. If the dialog has dismmiss normally I do nothin, or else I run the dialog again (and setting his state as when dismissing in my case).


builder.setCancelable(false);


public void Mensaje(View v){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("¿Quieres ir a el Menú principal?");
    builder.setMessage("Al presionar SI iras a el menú y saldras de la materia.");
    builder.setPositiveButton("SI", null);
    builder.setNegativeButton("NO", null);
    builder.setCancelable(false);
    builder.show();
}

Alert Dialog is deprecated so use Dialog dialog = new Dialog(this);

For prevent close on outside touch

dialog.setCanceledOnTouchOutside(false);

This is the perfect answer to all your questions.... Hope you enjoy coding in Android

new AlertDialog.Builder(this)
            .setTitle("Akshat Rastogi Is Great")
            .setCancelable(false)
            .setMessage("I am the best Android Programmer")
            .setPositiveButton("I agree", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            })
            .create().show();

참고URL : https://stackoverflow.com/questions/12102777/prevent-android-activity-dialog-from-closing-on-outside-touch

반응형