2014-09-20 48 views
1

美好的一天。我遇到問題,單擊按鈕時顯示一個消息框。簡單的消息框顯示註冊確認。之後,我打開新的活動。 問題是,它顯示消息框,然後啓動新的活動,而不用等待ok按鈕被點擊。如何在點擊確定按鈕後只顯示新的活動。如何在開始新活動之前等待好的按鈕

下面是我使用的代碼。

@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(); 
+0

添加'listener'爲'OK'點擊裏面那個你想要做什麼。 – Rustam 2014-09-20 10:42:01

回答

1

不要啓動活動在那裏。刪除行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並使用如上面給出。

根據您的風格,您不是將操作設置爲對話框,而是設置顯示對話框的按鈕。

+0

好吧,我是新來的機器人,所以我掙扎。思考,不能我有一些if語句,看看是否點擊確定按鈕,並把該startactivity如果statement.Because我不明白你的代碼 – user2893681 2014-09-20 10:46:19

+0

裏面的代碼DialogInterface.OnClickListener只會在用戶點擊按鈕時觸發。 – 2014-09-20 10:49:58

+0

你不需要檢查哪個按鈕被點擊。 setPositiveButton將檢查確定,setNegativeButton將檢查取消 – Nabin 2014-09-20 10:52:44

1

試試這個

@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); 
       } 
     }); 
    } 
}); 
+0

對不起,但我懷疑這是正確的。您在onClick中有dlgAlert,因此無法在外部使用它。 – Nabin 2014-09-20 10:45:26

+0

我編輯了我的答案。現在好嗎?請指出我是否做錯了 – 2014-09-20 10:57:37

+0

從我+1 :-)。 – Nabin 2014-09-20 11:28:08

相關問題