3

我的应用程序中有一个警报对话框,该对话框会在应用程序启动后立即启动。我alertDialog只希望在安装活动后首次启动活动时显示,除非用户删除应用程序然后再次安装它,否则我不希望alertDialog再次弹出。我试图看看如何实现这种方法并尝试学习编写正确的代码,但我似乎无法正确完成它。那么有人可以帮助我实现这种方法并解决这个问题。

    public class MainActivity extends Activity {


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

      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
    if (!initialDialogDisplayed) {
        Editor editor = preferences.edit();
        editor.putBoolean("InitialDialog", true);
        editor.commit();


final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setTitle("Alert");
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setMessage("Dialog");

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

            }
         });

        alertDialog.show();
                final EditText et = (EditText) findViewById(R.id.editText1);
                Button getAnswer = (Button) findViewById(R.id.button1);
                getAnswer.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {       
                        if (et.getText().toString().length()==0) {
                            Toast.makeText(getApplicationContext(),"Can't Be Blank!",Toast.LENGTH_LONG).show();             

                        }else{
                            EditText et = (EditText) findViewById(R.id.editText1);
                            String searchTerm = et.getText().toString().trim();         
                            Intent in = new Intent(MainActivity.this, ListView.class);
                            in.putExtra("TAG_SEARCH", searchTerm);
                            startActivity(in);
                        }

                    }
                });
            }
         }


        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }}
4

2 回答 2

3

您可以使用共享首选项来存储对话框已经显示的事实。您应该在对话框的显示上保存一个值并阅读它以检查是否需要再次显示。

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean initialDialogDisplayed = preferences.getBoolean("InitialDialog", false);
    if (!initialDialogDisplayed) {
        Editor editor = preferences.edit();
        editor.putBoolean("InitialDialog", true);
        editor.commit();

        // Display the dialog here
    }
于 2013-09-08T00:59:01.937 回答
3

您可以通过 SharedPreferences 存储标志值。

 SharedPreferences settings = getSharedPreferences("pref_name", 0);
 boolean installed = settings.getBoolean("installed", false);

if(!installed){

   //showDialog

   SharedPreferences.Editor editor = settings.edit();
   editor.putBoolean("installed", true);
   editor.commit();

}

链接:http: //developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-09-08T01:01:04.067 回答