2015-09-25 64 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

谢谢大家,我已经得到了答案,现在如何将更多的消息添加到同一个对话框?我想点击确定后,显示另一条消息。像这样。 –