2015-09-25 63 views
-3

我想要做的就是在我的android應用程序中添加一個AlertDialog,我希望當它被點擊到Popup至少10條消息時。就像JavaScript中的Popup消息一樣。如何添加Alertdialog並鏈接到活動按鈕?

+1

是什麼?你試試?到目前爲止顯示一些代碼。 http://stackoverflow.com/help/how-to-ask –

回答

0

這裏是如何添加警告對話框中您的應用程序

SharedPreferences mPrefs; 
final String welcomeScreenShownPref = "welcomeScreenShown"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this); 

    // second argument is the default to use if the preference can't be found 
    Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); 

    if (!welcomeScreenShown) { 
     // here you can launch another activity if you like 
     // the code below will display a popup 

     String whatsNewTitle = getResources().getString(R.string.whatsNewTitle); 
     String whatsNewText = getResources().getString(R.string.whatsNewText); 
     new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
       R.string.ok, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }).show(); 
     SharedPreferences.Editor editor = mPrefs.edit(); 
     editor.putBoolean(welcomeScreenShownPref, true); 
     editor.commit(); // Very important to save the preference 
    } 

} 
+0

請分解!這裏是我的按鈕的代碼<按鈕 的android:layout_width = 「WRAP_CONTENT」 機器人:layout_height = 「WRAP_CONTENT」 機器人:文本= 「戒律」 機器人:文字顏色= 「#ff0400」 機器人:textAllCaps = 「假」 機器人:id =「@ + id/Ten」 android:layout_centerHorizo​​ntal =「true」 android:layout_marginTop =「100dp」 android:onClick =「Ten」/>我在哪裏通過上面提供的代碼?在activity.xml中的類還是在哪裏? –

+0

謝謝大家,我已經得到了答案,現在如何將更多的消息添加到同一個對話框?我想點擊確定後,顯示另一條消息。像這樣。 –