начать активность с AlertDialog - PullRequest
0 голосов
/ 14 февраля 2020

Я хочу запустить действие из AlertDialog.Builder, но я не могу запустить какое-либо действие из OnclickListener диалогового интерфейса. из обоих setPositiveButton и setNegativeButton (пробовал 2 способа показать в качестве примера), оба они не работают и не запускаются либо menuactivity, либо exo5P1.

public class exo5 extends AppCompatActivity {
    private Button button;
    private EditText edt1, edt2, edt3, edt4, edt5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exo4);
        button = findViewById(R.id.button);
        edt1 = findViewById(R.id.editText);
        edt2 = findViewById(R.id.editText3);
        edt3 = findViewById(R.id.editText8);
        edt4 = findViewById(R.id.editText5);
        edt5 = findViewById(R.id.editText7);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder ValidationDialog = new AlertDialog.Builder(exo5.this);
                ValidationDialog.setTitle("Validation");
                ValidationDialog.setMessage("Voulez-vous vraiment valider").setCancelable(false).setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Vous Avez bien validé votre choix", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent( exo5.this,exo5P1.class);
                        startActivity(intent);

                    }
                }).setNegativeButton("Non", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), "Vous Avez annulé votre choix", Toast.LENGTH_LONG).show();
                        Intent intent =new Intent(exo5.this,menuActivity.class);
                        exo5.this.startActivity(intent);
                    }
                });

                 AlertDialog Dialog = ValidationDialog.create();
                Dialog.show();

            }
        });
    }
}

Я также пытался использовать getApplicationContext() в намерении, но это не работает.

1 Ответ

1 голос
/ 14 февраля 2020

Я попробовал это в своем проекте, и он запускает действие.

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder ValidationDialog = new AlertDialog.Builder(exo5.this);
                ValidationDialog.setTitle("Validation");
                ValidationDialog.setMessage("Voulez-vous vraiment valider").setCancelable(false).setPositiveButton("Oui", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent intent = new Intent(exo5.this, MyTestAct.class);
                        startActivity(intent);

                    }
                }).setNegativeButton("Non", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                AlertDialog Dialog = ValidationDialog.create();
                Dialog.show();

            }
        });
...