2012-03-03 80 views
18

我想創建一個「提示」對話框,通知用戶關於在手機上啓用GPS會降低電池壽命。我希望它彈出,但有一個複選框,上面寫着:「不要再問我了」。如何製作「不要再問我」對話框彈出框? Android

我該如何着手在Android中創建它?

謝謝,

Zukky。

AlertDialog.Builder prompt = new AlertDialog.Builder(this); 
prompt.setCancelable(false); 
prompt.setTitle("Warning"); 
prompt.setMessage ("HINT: Otherwise, it will use network to find" + 
        "your location. It's inaccurate but saves on " + 
        "battery! Switch GPS on for better accuracy " + 
        "but remember it uses more battery!"); 
+1

你試過了什麼? – 2012-03-03 21:33:40

+0

剛編輯的主要問題。我添加了^^,但是我怎樣才能讓它添加一個「不要再問我」複選框?它實際上不顯示它? – Zukky 2012-03-03 21:40:51

回答

40

編輯:當心!代碼重複提前。由於我不再爲Android開發,我無法重構下面的代碼。

它在Android偏好設置中設置一個值,並檢查它是否會顯示對話框。

checkbox.xml資源/佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <CheckBox 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/skip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ok please do not show again." > 
    </CheckBox> 
</LinearLayout> 

Activity.java

public class MyActivity extends Activity { 
    public static final String PREFS_NAME = "MyPrefsFile1"; 
    public CheckBox dontShowAgain; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    protected void onResume() { 
     AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     LayoutInflater adbInflater = LayoutInflater.from(this); 
     View eulaLayout = adbInflater.inflate(R.layout.checkbox, null); 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
     String skipMessage = settings.getString("skipMessage", "NOT checked"); 

     dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip); 
     adb.setView(eulaLayout); 
     adb.setTitle("Attention"); 
     adb.setMessage(Html.fromHtml("Zukky, how can I see this then?")); 

     adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       String checkBoxResult = "NOT checked"; 

       if (dontShowAgain.isChecked()) { 
        checkBoxResult = "checked"; 
       } 

       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
       SharedPreferences.Editor editor = settings.edit(); 

       editor.putString("skipMessage", checkBoxResult); 
       editor.commit(); 

       // Do what you want to do on "OK" action 

       return; 
      } 
     }); 

     adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       String checkBoxResult = "NOT checked"; 

       if (dontShowAgain.isChecked()) { 
        checkBoxResult = "checked"; 
       } 

       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
       SharedPreferences.Editor editor = settings.edit(); 

       editor.putString("skipMessage", checkBoxResult);      
       editor.commit(); 

       // Do what you want to do on "CANCEL" action 

       return; 
      } 
     }); 

     if (!skipMessage.equals("checked")) { 
      adb.show(); 
     } 

     super.onResume(); 
    } 
} 

As you can see, I did "copy and paste" too. Changed only the message strings. It works beautifully.

+0

不,它不起作用。如此多的錯誤。我完全遵循這一點,甚至複製和粘貼,並與它合作。還是行不通。任何人都知道其他方式? – Zukky 2012-03-04 00:07:12

+0

@Zukky我從以前鏈接的網站添加了代碼和佈局XML。還包括一個截圖。這在Android 2.2中進行了測試。希望能幫助到你。 – 2012-03-04 01:10:17

+0

作爲一個方面說明,也許你試圖使用帶有小寫標籤名稱的'checkbox.xml'。這將無法正常工作,但是Eclipse或任何其他IDE都顯示這些情況無效。 – 2012-03-04 01:17:10

7

你必須做出一個自定義對話框,例如在其上設置的自定義內容視圖的AlertDialog(與setView())。該自定義佈局可以是TextView(用於呈現信息)+ a CheckBox(用Do not ask me again)。在爲對話框的按鈕設置的OnClickListener中,您將獲得該狀態CheckBox,並且如果用戶選中它,而不是在首選項中設置標誌(例如,布爾值爲true)。

下一次用戶使用應用程序時,您會從首選項中檢查布爾值,如果它設置爲true,那麼您將不顯示對話框,否則用戶沒有檢查CheckBox,因此您向他顯示對話框再次。

編輯示例應用程序:

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.Toast; 

public class DoNotShowDialog extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Button action = new Button(this); 
     action.setText("Start the dialog if the user didn't checked the " 
       + "checkbox or if is the first run of the app."); 
     setContentView(action); 
     action.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       SharedPreferences prefs = PreferenceManager 
         .getDefaultSharedPreferences(DoNotShowDialog.this); 
       boolean dialog_status = prefs 
         .getBoolean("dialog_status", false);//get the status of the dialog from preferences, if false you ,ust show the dialog 
       if (!dialog_status) { 
        View content = getLayoutInflater().inflate(
          R.layout.dialog_content, null); // inflate the content of the dialog 
        final CheckBox userCheck = (CheckBox) content //the checkbox from that view 
          .findViewById(R.id.check_box1); 
        //build the dialog 
        new AlertDialog.Builder(DoNotShowDialog.this) 
          .setTitle("Warning") 
          .setView(content) 
          .setPositiveButton("Ok", 
            new DialogInterface.OnClickListener() { 

             public void onClick(
               DialogInterface dialog, 
               int which) { 
              //find our if the user checked the checkbox and put true in the preferences so we don't show the dialog again 
              SharedPreferences prefs = PreferenceManager 
                .getDefaultSharedPreferences(DoNotShowDialog.this); 
              SharedPreferences.Editor editor = prefs 
                .edit(); 
              editor.putBoolean("dialog_status", 
                userCheck.isChecked()); 
              editor.commit(); 
              dialog.dismiss(); //end the dialog. 
             } 
            }) 
          .setNegativeButton("Cancel", 
            new DialogInterface.OnClickListener() { 

             public void onClick(
               DialogInterface dialog, 
               int which) { 
              //find our if the user checked the checkbox and put true in the preferences so we don't show the dialog again 
              SharedPreferences prefs = PreferenceManager 
                .getDefaultSharedPreferences(DoNotShowDialog.this); 
              SharedPreferences.Editor editor = prefs 
                .edit(); 
              editor.putBoolean("dialog_status", 
                userCheck.isChecked()); 
              editor.commit(); 
              dialog.dismiss(); 

             } 
            }).show(); 
       } else { 
        //the preferences value is true so the user did checked the checkbox, so no dialog 
        Toast.makeText(
          DoNotShowDialog.this, 
          "The user checked the checkbox so we don't show the dialog any more!", 
          Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
    } 
} 

並且對話的內容佈局(R.layout.dialog_content):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enabling GPS on your phone will decrease battery life!" /> 

    <CheckBox 
     android:id="@+id/check_box1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Do not ask me again!" /> 

</LinearLayout> 
+0

啊,我明白了。讓我試試看,我會回到你的隊友。 – Zukky 2012-03-03 21:43:04

+0

你可以在代碼中顯示這個嗎?我不能讓它工作夥伴.. – Zukky 2012-03-04 00:21:25

+0

@Zukky我做了一個示例應用程序。檢查我的答案。 – Luksprog 2012-03-04 07:44:21

2

我有我的代碼更少的解決方案。這並不完美,因爲不能使用描述,只有信息可以作爲對話的標題傳遞。 MultiChoiceItem用於複選框。

res/values/strings。XML:

<string-array name="do_not_show_again_array"> 
    <item>Do not show again.</item> 
</string-array> 

然後我的代碼看起來如下:

DialogInterface.OnClickListener dialogClickListener = new OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // Do something here 
    } 
}; 
final AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
AlertDialog alertDialog = builder.setTitle("Title/Description") 
     .setMultiChoiceItems(R.array.do_not_show_again_array, null, new OnMultiChoiceClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
       appPrefs.setLocationOnStart(!isChecked); 
      } 
     }) 
     .setPositiveButton("Ja", dialogClickListener) 
     .setNegativeButton("Nein", dialogClickListener).show(); 
} 
0

您好我跟着tutorial我發現這個代碼
你可以用下面這段代碼:

AlertDialog.Builder adb= new 

AlertDialog.Builder(this); 
    LayoutInflater adbInflater = 

LayoutInflater.from(this); 
    View eulaLayout = adbInflater.inflate 

(R.layout.activity_main, null); 
    check = (CheckBox) 

eulaLayout.findViewById(R.id.skip); 
    adb.setView(eulaLayout); 
    adb.setTitle("Example:"); 
    adb.setMessage(Html.fromHtml("Type your 

    text here: ")); 
    adb.setPositiveButton("Ok", new 

    DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface 

dialog, int which) { 
      String checkBoxResult = "NOT 

checked"; 
      if (check.isChecked()) 

checkBoxResult = "checked"; 
       SharedPreferences settings = 

getSharedPreferences(PREFS_NAME, 0); 
       SharedPreferences.Editor 

editor = settings.edit(); 
       editor.putString("noshow", 

checkBoxResult); 
       // Commit the edits! 

      // sunnovalthesis(); 

       editor.commit(); 
      return; 
     } }); 

     adb.setNegativeButton("Cancel", new 

    DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface 

    dialog, int which) { 
     String checkBoxResult = "NOT 

    checked"; 
     if (check.isChecked()) 

     checkBoxResult = "checked"; 
     SharedPreferences settings = 

     getSharedPreferences(PREFS_NAME, 0); 
      SharedPreferences.Editor editor = 

     settings.edit(); 
      editor.putString("noshow", 

    checkBoxResult); 
      // Commit the edits! 

     // sunnovalthesis(); 

      editor.commit(); 
      return; 
    } }); 
    SharedPreferences settings = 

    getSharedPreferences(PREFS_NAME, 0); 
    String noshow = settings.getString 

    ("noshow", "NOT checked"); 
     if (noshow != "checked") adb.show(); 
0

我對這個問題有一個清晰和正確的方法

package com.example.user.testing; 

import android.content.DialogInterface; 
import android.content.SharedPreferences; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.CheckBox; 

public class MainActivity extends AppCompatActivity { 
CheckBox dontShowAgain; 
public static final String PREFS_NAME = "MyPrefsFile1"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final AlertDialog.Builder adb = new    AlertDialog.Builder(MainActivity.this); 
    LayoutInflater adbInflater = LayoutInflater.from(MainActivity.this); 
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null); 

    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip); 
    adb.setView(eulaLayout); 
    adb.setTitle("Attention"); 
    adb.setMessage("Your message here"); 
    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 


      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
      SharedPreferences.Editor editor = settings.edit(); 
      editor.putBoolean("skipMessage", dontShowAgain.isChecked()); 
      editor.commit(); 
      dialog.cancel(); 
     } 
    }); 

    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    Boolean skipMessage = settings.getBoolean("skipMessage", false); 
    if (skipMessage.equals(false)) { 
     adb.show(); 
    } 

} 

}``