Android Dialog對話框?qū)嵗a講解
Dialog的基本方法
//創(chuàng)建Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
//設(shè)置標(biāo)題圖標(biāo)
builder.setIcon(R.drawable.ic_launcher);
//設(shè)置標(biāo)題
builder.setTitle("這是一個(gè)對話框");
//設(shè)置信息
builder.setMessage("是否要跳轉(zhuǎn)?");
//確定按鈕
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//取消按鈕
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//忽略
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//顯示對話框
show();
系統(tǒng)樣式
1.下拉列表
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("選擇一個(gè)城市");
//下拉列表的數(shù)據(jù)源
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 = {"男", "女"};
//第二個(gè)參數(shù)指定默認(rèn)哪個(gè)單選框被勾中
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 = {"廷禧攻略", "扶搖", "香蜜沉沉燼如霜", "如懿傳"};
//第二個(gè)參數(shù)代表哪幾個(gè)選項(xiàng)被選擇,需要傳遞一個(gè)boolean[]數(shù)組進(jìn)去,其長度要和第一個(gè)參數(shù)的長度相同,如果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);//采用不確定式的進(jìn)度條
waitingDialog.setCancelable(false);//點(diǎn)擊外部不取消對話框
waitingDialog.show();
5.進(jìn)度條對話框
int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgress(0);//設(shè)置默認(rèn)值
progressDialog.setTitle("正在下載");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//進(jìn)度條樣式
progressDialog.setMax(MAXPD);//設(shè)置最大值
progressDialog.show();
自定義Dialog
1.繼承Dialog
public class CustomDialog extends Dialog {
//標(biāo)題
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);
}
//置右鍵文字和點(diǎn)擊事件
public void setRightButton(String rightStr, View.OnClickListener clickListener) {
doubleRightBtn.setOnClickListener(clickListener);
doubleRightBtn.setText(rightStr);
}
//設(shè)置左鍵文字和點(diǎn)擊事件
public void setLeftButton(String leftStr, View.OnClickListener clickListener) {
doubleLeftBtn.setOnClickListener(clickListener);
doubleLeftBtn.setText(leftStr);
}
//設(shè)置提示內(nèi)容
public void setHintText(String str) {
hintTv.setText(str);
hintTv.setVisibility(View.VISIBLE);
}
//給兩個(gè)按鈕 設(shè)置文字
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>
<!-- 無標(biāo)題 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮現(xiàn)在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時(shí)隱藏底部虛擬按鍵
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();
總結(jié)
以上所述是小編給大家介紹的Android Dialog對話框?qū)嵗a講解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android自定義對話框Dialog的簡單實(shí)現(xiàn)
- Android編程自定義對話框(Dialog)位置及大小的方法
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對話框的功能
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對話框功能
- Android實(shí)現(xiàn)底部對話框BottomDialog彈出實(shí)例代碼
- Android實(shí)現(xiàn)自定義圓角對話框Dialog的示例代碼
- Android時(shí)間對話框TimePickerDialog詳解
- Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對話框效果(7)
相關(guān)文章
Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能
這篇文章主要介紹了Android使用系統(tǒng)自帶的相機(jī)實(shí)現(xiàn)一鍵拍照功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Android ListView自動顯示隱藏布局的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ListView自動顯示隱藏布局的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例
這篇文章主要介紹了Android中使用ContentProvider管理系統(tǒng)資源的實(shí)例,講解了ContentProvider對系統(tǒng)中聯(lián)系人及多媒體資源的管理例子,需要的朋友可以參考下2016-04-04
Android ContentProvider基礎(chǔ)應(yīng)用詳解
ContentProvider是android四大組件之一。它是不同應(yīng)用程序之間交換數(shù)據(jù)的標(biāo)準(zhǔn)api,ContentProvider以某種uri的形式對外提供數(shù)據(jù),允許其它應(yīng)用程序?qū)ζ湓L問或者修改數(shù)據(jù)。本文將介紹ContentProvider的基礎(chǔ)應(yīng)用,感興趣的可以學(xué)習(xí)一下2021-12-12
Android入門之Gallery+ImageSwitcher用法實(shí)例解析
這篇文章主要介紹了Android入門之Gallery+ImageSwitcher用法,對Android初學(xué)者有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Android自定義ViewGroup的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05
Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)點(diǎn)擊EditText之外的控件隱藏軟鍵盤功能,涉及Android控件的功能、屬性及相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android自定義控件實(shí)現(xiàn)望遠(yuǎn)鏡效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)望遠(yuǎn)鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

