標籤:android 對話方塊 checkbox dialog
android_checkbox_dialog設計是不是開起預設不提示
package com.example.android_checkbox_dialog;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnMultiChoiceClickListener;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class TestDialogActivity extends Activity { private Button btn; final Context context = this; //定義複選框選項 final String[] multiChoiceItems = {"開啟預設不提示"}; //複選框預設值:false=未選;true=選中 ,各自對應items[i] final boolean[] defaultSelectedStatus = {false}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.Button01); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(defaultSelectedStatus[0] != true) showDialog5(); else Toast.makeText(context,"已經開啟預設設定", Toast.LENGTH_LONG).show(); } }); } public void showDialog5(){ final StringBuilder sb = new StringBuilder(); //建立對話方塊 new AlertDialog.Builder(context) .setTitle("複選框")//設定對話方塊標題 .setMultiChoiceItems(multiChoiceItems, defaultSelectedStatus, new OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //來回重複選擇取消,得相應去改變item對應的bool值,點擊確定時,根據這個bool[],得到選擇的內容 defaultSelectedStatus[which] = isChecked; } }) //設定對話方塊[肯定]按鈕 .setPositiveButton("確定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { for(int i=0;i<defaultSelectedStatus.length;i++) { if(defaultSelectedStatus[i]) { sb.append(multiChoiceItems[i]); } } // TODO Auto-generated method stub Toast.makeText(context,sb.toString(), Toast.LENGTH_LONG).show(); } }) .setNegativeButton("取消", null)//設定對話方塊[否定]按鈕 .show(); } }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
android_checkbox_dialog 設計 是不是要開起 預設不提示對話方塊