我的应用程序中有一个警报对话框,该对话框会在应用程序启动后立即启动。我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();
}}