簡體   English   中英

如何在開始新活動之前等待“確定”按鈕

[英]How to wait for an okay button before starting a new activity

美好的一天,我有一個問題,單擊按鈕時會顯示一個消息框。簡單的消息框顯示注冊確認,然后打開一個新活動。 問題是它顯示了消息框,然后啟動新活動,而沒有等待單擊確定按鈕。如何僅在單擊確定按鈕后顯示新活動。

下面是我使用的代碼。

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button btn = (Button)findViewById(R.id.registerButton);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);

                dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
                dlgAlert.setTitle("Registration");
                dlgAlert.setPositiveButton("OK", null);
                dlgAlert.setCancelable(false);
                dlgAlert.create().show();


                startActivity(intent);
                finish();
            }
        });

將代碼更改為

 @Override
            public void onClick(View view) {
               // Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);

                dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
                dlgAlert.setTitle("Registration");
                dlgAlert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
                        startActivity(intent);
                    }
                });
                dlgAlert.setCancelable(false);
                dlgAlert.show();

不要在那里開始活動。 刪除行startActivity(intent)finish() 你需要這樣做

builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent(your arguments here) 
        startActivity(intent);
     }
});
builder.show();

因此,您需要做的就是將行更改為setPositiveButton並按照上面的說明使用。

根據您的樣式,您不是在對話框上設置操作,而是在顯示對話框的按鈕上設置操作。

嘗試這個

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button btn = (Button)findViewById(R.id.registerButton);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(context);
                dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
                dlgAlert.setTitle("Registration");
                dlgAlert.setPositiveButton("OK", null);
                dlgAlert.setCancelable(false);
                dlgAlert.create().show();
                dlgAlert.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
                    startActivity(intent); 
               }
         });
     }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM