Android中Dialog對(duì)話框的使用小結(jié)
前言
最近有些空時(shí)間,所以花了一個(gè)小時(shí)對(duì)Dialog對(duì)話框使用小結(jié)一下,比較基礎(chǔ),希望對(duì)你學(xué)習(xí)有幫助,大牛請(qǐng)直接關(guān)閉網(wǎng)頁(yè)。如果你是新手,建議你親自敲一遍代碼。
先看一下效果:

Dialog對(duì)話框使用小結(jié)
一、普通對(duì)話框
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("溫馨提示");//標(biāo)題
builder.setMessage("天氣冷,注意保暖");
builder.setIcon(R.mipmap.ic_launcher);
builder.create();
builder.show();

普通對(duì)話框
二、確定取消對(duì)話框
builder.setTitle("確定取消對(duì)話框");
builder.setMessage("請(qǐng)選擇確定或取消");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
//正能量按鈕 Positive
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你點(diǎn)擊了確定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你選擇了取消", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();

確定取消對(duì)話框
三、多按鈕對(duì)話框
builder.setTitle("多個(gè)按鈕對(duì)話框");
builder.setMessage("請(qǐng)選擇");
builder.setIcon(R.mipmap.ic_launcher);
builder.setPositiveButton("我沒(méi)玩夠", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "繼續(xù)瀏覽精彩內(nèi)容", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("開(kāi)啟", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "起床了", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("我累了,要休息一下", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "歡迎再來(lái)", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();

多按鈕對(duì)話框
四、列表對(duì)話框
final String arrItem[] = getResources().getStringArray(R.array.aikaifa);
builder.setItems(arrItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你選擇了第" + arrItem[which], Toast.LENGTH_SHORT).show();
}
});
builder.create().show();

列表對(duì)話框
五、帶Adapter的對(duì)話框
builder.setTitle("帶Adapter的對(duì)話框");
builder.setIcon(R.mipmap.ic_launcher);
final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
int arrImg[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
for (int i = 0; i < arrImg.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", arrImg[i]);
map.put("title", "愛(ài)開(kāi)發(fā)" + i);
list.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(activity, list, R.layout.list_item, new String[]{"img", "title"}, new int[]{R.id.iv, R.id.tv});
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, "你選擇了" + list.get(which).get("title").toString().trim(), Toast.LENGTH_SHORT).show();
}
});
builder.create().show();

帶Adapter的對(duì)話框
六、單選對(duì)話框
builder.setTitle("單選對(duì)話框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(R.array.aikaifa, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(activity, which+"", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();

單選對(duì)話框
七、多選對(duì)話框
builder.setTitle("多選對(duì)話框");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(R.array.aikaifa, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(activity, which+""+isChecked, Toast.LENGTH_SHORT).show();
}
});
builder.create().show();

多選對(duì)話框
八、日期對(duì)話框
DatePickerDialog datePickerDialog=new DatePickerDialog(activity,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(activity,
year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show();
}
},
2017, 02, 9);
datePickerDialog.show();

日期對(duì)話框
九、時(shí)間對(duì)話框
TimePickerDialog timePickerDialog=new TimePickerDialog(activity,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(activity,
hourOfDay+"小時(shí)"+minute+"分鐘", Toast.LENGTH_SHORT).show();
}
},
17, 49, true);
timePickerDialog.show();

時(shí)間對(duì)話框
十、自定義對(duì)話框
View view= LayoutInflater.from(activity).inflate(R.layout.dialog_login, null);
builder.setView(view);
builder.create();
final EditText et_phone=(EditText)view.findViewById(R.id.et_phone);
final EditText et_password=(EditText)view.findViewById(R.id.et_password);
Button btn_submit=(Button)view.findViewById(R.id.btn_submit);
btn_submit.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, "手機(jī)號(hào)碼:"+et_phone.getText().toString()+" 短信驗(yàn)證碼:"+et_password.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.show();

自定義對(duì)話框
項(xiàng)目設(shè)計(jì)到的xml
list_item.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:background="#f5f5f5" android:orientation="horizontal" android:padding="10dp"> <ImageView android:id="@+id/iv" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:text="標(biāo)題" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
dialog_login.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="8dp" android:orientation="vertical" android:padding="5dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:text="驗(yàn)證手機(jī)號(hào)碼" android:textColor="#414141" /> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:hint="請(qǐng)輸入手機(jī)號(hào)碼" android:inputType="number" android:maxLength="11" android:paddingLeft="10dp" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="10dp"> <EditText android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="4" android:gravity="center_vertical" android:hint="請(qǐng)輸入短信驗(yàn)證碼" android:inputType="number" android:maxLength="6" android:paddingLeft="10dp" android:textSize="14sp" /> <TextView android:id="@+id/tv_get_code" android:layout_width="0dp" android:layout_height="48dp" android:layout_marginLeft="10dp" android:layout_weight="2" android:enabled="false" android:gravity="center" android:text="點(diǎn)擊獲取" android:textColor="#000000" /> </LinearLayout> <Button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:gravity="center" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="提交" /> </LinearLayout> </RelativeLayout>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- 8種android 對(duì)話框(Dialog)使用方法詳解
- 屬于自己的Android對(duì)話框(Dialog)自定義集合
- Android中制作自定義dialog對(duì)話框的實(shí)例分享
- Android Dialog對(duì)話框詳解
- Android 對(duì)話框 Dialog使用實(shí)例講解
- Android Dialog對(duì)話框用法實(shí)例詳解
- Android Dialog 對(duì)話框詳解及示例代碼
- Android 對(duì)話框(Dialog)大全示例(建立你自己的對(duì)話框)
相關(guān)文章
Android開(kāi)發(fā)adb.exe'' and can be executed.錯(cuò)誤解決方法
這篇文章主要介紹了Android開(kāi)發(fā)adb.exe' and can be executed.錯(cuò)誤解決方法,本文分析了問(wèn)題的可能原因并給出了排查步驟,需要的朋友可以參考下2015-06-06
Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能,結(jié)合實(shí)例形式分析了Android ImageSwitcher相冊(cè)的原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03
Android使用相機(jī)實(shí)現(xiàn)拍照存儲(chǔ)及展示功能詳解
這篇文章主要介紹了Android使用相機(jī)實(shí)現(xiàn)拍照存儲(chǔ)及展示功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
Android NDK開(kāi)發(fā)簡(jiǎn)單程序分享(Hello Word!)
本文主要對(duì)Android NDK開(kāi)發(fā)簡(jiǎn)單程序(Hello Word!)的實(shí)現(xiàn)步驟及方法進(jìn)行詳細(xì)介紹。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
Android GridView實(shí)現(xiàn)動(dòng)畫(huà)效果實(shí)現(xiàn)代碼
這篇文章主要介紹了 Android GridView實(shí)現(xiàn)動(dòng)畫(huà)效果實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
android實(shí)現(xiàn)倒計(jì)時(shí)功能的方法
這篇文章主要為大家詳細(xì)介紹了兩種android實(shí)現(xiàn)倒計(jì)時(shí)功能的方法,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-08-08
Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例
這篇文章主要介紹了Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

