2013-09-24 89 views
1

我有一個對話框窗口,它必須有容器和兩個點擊偵聽器來關閉對話框 我希望我只顯示一次,而我使用這段代碼但它根本不顯示它!只顯示一次對話框

有幫助嗎?

SharedPreferences setting = getSharedPreferences("dialogprefs", 0); 
    final SharedPreferences.Editor editor = setting.edit(); 
    { 
      final Dialog dialog = new Dialog(this); 
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
      dialog.setContentView(R.layout.hint1); 
      dialog.setCanceledOnTouchOutside(true); 
      dialog.setTitle("Hint"); 
      View masterView = dialog.findViewById(R.id.LinearLayout1); 
      masterView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        dialog.dismiss(); 
        editor.putString("dialogshow", "0".toString()); 
        editor.commit(); 
       } 
      }); 
       View masterView1 = dialog.findViewById(R.id.imageView1); 
       masterView1.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         dialog.dismiss(); 
         editor.putString("dialogshow", "0".toString()); 
         editor.commit(); 
        } 
      }); 
      if (setting.getString("dialogshow" , "0").equals("0")) { 
       dialog.dismiss(); 
      } 
      else { 
      dialog.show(); 
      } 
    } 
+0

你有沒有試過調試你的應用程序? – Tobrun

+0

爲什麼你使用字符串,它的混淆..爲什麼你不保存布爾值,這將有意義,在它顯示之前是假的,真正的後?/ –

+0

謝謝!但我的問題是設置一個空的sharedprfs – Mahfa

回答

0

假設setting.getString("dialogShow", "0")意味着像getString(name, defaultValue),則默認爲"0"如果偏好已尚未設置,這意味着該對話框將永遠不會被顯示即使一次。也許你的意思是setting.getString("dialogShow", "1")