我們來舉一個例子:
每次啟動應用程式(APP),都詢問是否瀏覽GOOGLE首頁,
當使用者勾選"不再顯示",下一次進入到程式時,即不會再顯示此對話框。
MainActivity.java
package com.example.time; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.view.LayoutInflater; import android.view.View; import android.widget.CheckBox; public class MainActivity extends Activity { boolean skipMessage; AlertDialog.Builder alert; public CheckBox dontShowAgain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 宣告對話框 CKDiagbox(); // 如果沒有勾選"不再顯示",則顯示對話框 if (!isCheckboxStateEnabled()) alert.show(); } public void CKDiagbox() { alert = new AlertDialog.Builder(this); LayoutInflater adbInflater = LayoutInflater.from(this); View checkboxLayout = adbInflater.inflate(R.layout.checkbox, null); dontShowAgain = (CheckBox) checkboxLayout.findViewById(R.id.skip); alert.setView(checkboxLayout); alert.setTitle(R.string.settitle); alert.setIcon(android.R.drawable.ic_dialog_alert); alert.setMessage(R.string.toGoogle); alert.setPositiveButton("離開", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { boolean checkBoxResult = false; if (dontShowAgain.isChecked()) checkBoxResult = true; setCheckboxState(checkBoxResult); // 關閉對話框 finish(); } }); alert.setNegativeButton("進入", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { boolean checkBoxResult = false; if (dontShowAgain.isChecked()) checkBoxResult = true; setCheckboxState(checkBoxResult); Uri uri = Uri.parse("http://www.google.com/"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); } }); } public void setCheckboxState(boolean chk) { // 記錄勾選方塊是否被打勾 SharedPreferences settings = getSharedPreferences("showit", 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("skipMessage", chk); editor.commit(); } public boolean isCheckboxStateEnabled() { // 讀取勾選方塊是否被打勾,預設值是未打勾(fasle) SharedPreferences settings = getSharedPreferences("showit", 0); skipMessage = settings.getBoolean("skipMessage", false); return skipMessage; } }加入核取方塊 res/layout/checkbox.xml
<?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="horizontal" android:paddingLeft="10dp" android:paddingRight="10dp" > <CheckBox android:id="@+id/skip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkbox" > </CheckBox> </LinearLayout>res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">checkbox dialog box</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="checkbox">不在顯示</string> <string name="toGoogle">是否前往GOOGLE首頁</string> <string name="settitle">歡迎</string> </resources>結果:
沒有留言:
張貼留言