Dialog的基本方法 系統樣式 1.下拉列表 2.單選框列表 3.多選框列表 4.等待對話框 5.進度條對話框 自定義Dialog 1.繼承Dialog 2.自定義Style 3.自定義佈局 4.ipad隱藏底部虛擬按鍵 5.使用自定義Dialog ...
Dialog的基本方法
//創建Dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //設置標題圖標 builder.setIcon(R.drawable.ic_launcher); //設置標題 builder.setTitle("這是一個對話框"); //設置信息 builder.setMessage("是否要跳轉?"); //確定按鈕 setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) //取消按鈕 setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener) //忽略 setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener) //顯示對話框 show();
系統樣式
1.下拉列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("選擇一個城市"); //下拉列表的數據源 final String[] cities = {"北京", "上海", "廣州", "深圳", "杭州"}; builder.setItems(cities, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(MainActivity.this, "你選擇的城市為:" + cities[which], Toast.LENGTH_SHORT).show(); } }); builder.show();
2.單選框列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("請選擇性別"); final String[] sex = {"男", "女"}; //第二個參數指定預設哪個單選框被勾中 builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(MainActivity.this, "性別為:" + sex[which], Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.show();
3.多選框列表
String str;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("選擇你想看的電視劇"); final String[] hobbies = {"廷禧攻略", "扶搖", "香蜜沉沉燼如霜", "如懿傳"}; //第二個參數代表哪幾個選項被選擇,需要傳遞一個boolean[]數組進去,其長度要和第一個參數的長度相同,如果null表示都不選 builder.setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked){ if(isChecked){ str.append(hobbies[which] + ", "); } Toast.makeText(MainActivity.this, "選擇的是:" + str.toString(), Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("確定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ } }); builder.show();
4.等待對話框
ProgressDialog waitingDialog= new ProgressDialog(MainActivity.this); waitingDialog.setTitle("等待載入中,請稍後..."); waitingDialog.setMessage("等待中..."); waitingDialog.setIndeterminate(true);//採用不確定式的進度條 waitingDialog.setCancelable(false);//點擊外部不取消對話框 waitingDialog.show();
5.進度條對話框
int MAXPD = 100; ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgress(0);//設置預設值 progressDialog.setTitle("正在下載"); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//進度條樣式 progressDialog.setMax(MAXPD);//設置最大值 progressDialog.show();
自定義Dialog
1.繼承Dialog
public class CustomDialog extends Dialog { //標題 protected TextView hintTv; //左邊按鈕 protected Button doubleLeftBtn; //右邊按鈕 protected Button doubleRightBtn; //輸入框 public EditText editText; public CustomDialog(Context context) { super(context, R.style.CustomDialogStyle); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setCancelable(false); // 是否可以撤銷 setContentView(R.layout.dialog_custom); hintTv = findViewById(R.id.tv_common_dialog_hint); doubleLeftBtn = findViewById(R.id.btn_common_dialog_double_left); doubleRightBtn = findViewById(R.id.btn_common_dialog_double_right); editText = findViewById(R.id.et_common_dialog); } //置右鍵文字和點擊事件 public void setRightButton(String rightStr, View.OnClickListener clickListener) { doubleRightBtn.setOnClickListener(clickListener); doubleRightBtn.setText(rightStr); } //設置左鍵文字和點擊事件 public void setLeftButton(String leftStr, View.OnClickListener clickListener) { doubleLeftBtn.setOnClickListener(clickListener); doubleLeftBtn.setText(leftStr); } //設置提示內容 public void setHintText(String str) { hintTv.setText(str); hintTv.setVisibility(View.VISIBLE); } //給兩個按鈕 設置文字 public void setBtnText(String leftStr, String rightStr) { doubleLeftBtn.setText(leftStr); doubleRightBtn.setText(rightStr); } }
2.自定義Style
<style name="CustomDialogStyle" parent="@android:style/Theme.Dialog"> <!-- 邊框 --> <item name="android:windowFrame">@null</item> <!-- 背景透明 --> <item name="android:windowBackground">@color/transparent</item> <!-- 無標題 --> <item name="android:windowNoTitle">true</item> <!-- 是否浮現在activity之上 --> <item name="android:windowIsFloating">true</item> <!-- 半透明 --> <item name="android:windowIsTranslucent">false</item> <!-- 背景模糊 --> <item name="android:windowContentOverlay">@null</item> <!-- 允許對話框的背景變暗 --> <item name="android:backgroundDimEnabled">true</item> <!-- 字體顏色 --> <item name="android:textColor">@color/white</item> <item name="android:editTextColor">@color/white</item> </style>
3.自定義佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll_common_dialog_layout" android:layout_width="500dp" android:layout_height="250dp" android:layout_margin="5dp" android:background="@drawable/background_info" android:orientation="vertical" android:gravity="center"> <!--提示信息--> <TextView android:id="@+id/tv_common_dialog_hint" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/white" android:textSize="27sp"/> <EditText android:id="@+id/et_common_dialog" android:layout_width="300dp" android:layout_height="wrap_content" android:textColor="@color/back" android:inputType="text|textNoSuggestions" tools:ignore="LabelFor" android:hint="請輸入密碼"/> <!--底部按鈕--> <LinearLayout android:id="@+id/ll_common_dialog_double" android:layout_width="360dp" android:layout_height="60dp" android:layout_margin="20dp" android:orientation="horizontal"> <Button android:id="@+id/btn_common_dialog_double_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/btnselector" android:gravity="center" android:textColor="@color/white" android:textSize="27dp"/> <Button android:id="@+id/btn_common_dialog_double_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/btnselector" android:gravity="center" android:textColor="@color/white" android:textSize="27dp"/> </LinearLayout> </LinearLayout>
4.ipad隱藏底部虛擬按鍵
//彈出dialog時隱藏底部虛擬按鍵 dialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); dialog.getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; if (Build.VERSION.SDK_INT >= 19) { uiOptions |= 0x00001000; } else { uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE; } dialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions); } });
5.使用自定義Dialog
CustomDialog dialog = new CustomDialog(this); dialog.setHintText("請輸入密碼"); dialog.setLeftButton("取消", new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.setRightButton("確定", new View.OnClickListener() { @Override public void onClick(View v) { } }); dialog.show();