Android UI設(shè)計(jì)之AlertDialog彈窗控件
有關(guān)android的彈窗界面相信大家見過不少了,手機(jī)上很多應(yīng)用軟件都涉及到彈窗控件,比如典型的每次刪除一個(gè)圖片或者卸載一個(gè)等都會(huì)彈出一個(gè)窗口詢問是否刪除/卸載等,還有我們系統(tǒng)的設(shè)置時(shí)間/日期等,都用到了這樣的控件,下面我將通過代碼來總結(jié)下常用的幾個(gè)彈窗控件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.company.alertdialog.MainActivity"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="列表彈窗" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="單選彈窗" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="多選彈窗" /> <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="日期彈窗" /> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="時(shí)間彈窗" /> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="進(jìn)度條彈窗" /> </LinearLayout>
strings.xml
<resources> <string name="app_name">AlertDialog</string> <string-array name="list"> <item>列表一</item> <item>列表二</item> <item>列表三</item> <item>列表四</item> <item>列表五</item> <item>列表六</item> </string-array> </resources>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//表示列表彈窗
private Button mBtn1;
//表示單選彈窗
private Button mBtn2;
//表示多選彈窗
private Button mBtn3;
//表示日期彈窗
private Button mBtn4;
//表示時(shí)間彈窗
private Button mBtn5;
//表示進(jìn)度條彈窗
private Button mBtn6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
event();
}
/**
* 設(shè)置監(jiān)聽事件
*/
private void event() {
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
mBtn5.setOnClickListener(this);
mBtn6.setOnClickListener(this);
}
/**
* 初始化控件
*/
private void init() {
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn3 = (Button) findViewById(R.id.btn3);
mBtn4 = (Button) findViewById(R.id.btn4);
mBtn5 = (Button) findViewById(R.id.btn5);
mBtn6 = (Button) findViewById(R.id.btn6);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
createListDialog();
break;
case R.id.btn2:
createSingleDialog();
break;
case R.id.btn3:
createMutilDialog();
break;
case R.id.btn4:
createDateDialog();
break;
case R.id.btn5:
createTimeDialog();
break;
case R.id.btn6:
createProgressBarDialog();
break;
}
}
/**
* 創(chuàng)建一個(gè)進(jìn)度條彈窗
*/
private void createProgressBarDialog() {
//創(chuàng)建進(jìn)度條彈窗對象
ProgressDialog progressDialog = new ProgressDialog(this);
//設(shè)置標(biāo)題
progressDialog.setTitle("進(jìn)度條彈窗");
//設(shè)置標(biāo)題圖標(biāo)
progressDialog.setIcon(R.mipmap.ic_launcher);
//設(shè)置文本
progressDialog.setMessage("正在加載...");
//設(shè)置最大進(jìn)度
progressDialog.setMax(100);
//設(shè)置進(jìn)度條的類型
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//顯示進(jìn)度條彈窗
progressDialog.show();
//如果設(shè)置這條語句的話,那么無論你點(diǎn)擊屏幕外的任何地方或者按返回鍵都取消不了這個(gè)彈窗,
//除非在完成進(jìn)度后,設(shè)置取消事件。一般情況這種設(shè)置方式對界面很不友好
//不過有時(shí)候軟件有重大bug,用戶不得不更新該軟件,如果不更新,就只能
//強(qiáng)制退出程序了
// progressDialog.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
progressDialog.setProgress(50);
}
/**
* 創(chuàng)建一個(gè)日期彈窗
*/
private void createDateDialog() {
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
/**
*
* @param view 當(dāng)前日期選擇的 view
* @param year 當(dāng)前選擇的年
* @param monthOfYear 當(dāng)前選擇的月,從0開始算
* @param dayOfMonth,當(dāng)前選擇的日,從1開始算
*/
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(MainActivity.this, "view = " + view + "年:" + year + "月:" + monthOfYear + "日" + dayOfMonth, Toast.LENGTH_SHORT).show();
}
}, 2016, 7, 15)//這里注意一下的是月份系統(tǒng)表示的是從0開始的,0表示1月,1表示2月.....11表示12月
.show();
}
/**
* 創(chuàng)建一個(gè)時(shí)間彈窗
*/
private void createTimeDialog() {
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
/**
*
* @param view 當(dāng)前時(shí)間選擇的view
* @param hourOfDay 小時(shí)
* @param minute 分鐘
*/
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, "時(shí)間彈窗 view = " + view + "hourOfDay = " + hourOfDay + "minute = " + minute, Toast.LENGTH_SHORT).show();
}
}, 11, 22, true)
.show();
}
/**
* 創(chuàng)建一個(gè)多選彈窗
*/
private void createMutilDialog() {
new AlertDialog.Builder(this)
.setTitle("多選彈框")
.setIcon(R.mipmap.ic_launcher)
//第二個(gè)參數(shù) boolean數(shù)組, 如果寫 null 代表默認(rèn)全部是非選中, 如果想指定某幾個(gè)選中,
//需要?jiǎng)?chuàng)建對應(yīng)長度的數(shù)據(jù),按照位置的順序,將指定位置設(shè)置為 true 即可, 數(shù)組長度不能小
//于數(shù)據(jù)源的長度,否則會(huì)越界,但是可以大于數(shù)據(jù)源的長度
.setMultiChoiceItems(R.array.list, new boolean[]{true, false, false, true, false, false, false, false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
/**
*
* @param dialog 當(dāng)前點(diǎn)擊的對話框
* @param which 當(dāng)前點(diǎn)擊的條目
* @param isChecked 被點(diǎn)擊條目的選中狀態(tài)
*/
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, "當(dāng)前點(diǎn)擊的是" + which + " 是否選中" + isChecked, Toast.LENGTH_SHORT).show();
}
})
//設(shè)置取消按鈕,并且設(shè)置監(jiān)聽事件
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
//確認(rèn)按鈕,默認(rèn)點(diǎn)擊會(huì)直接取消該窗口
.setPositiveButton("sure", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setCancelable(false)
.show();
}
/**
* 創(chuàng)建一個(gè)單選彈窗
*/
private void createSingleDialog() {
new AlertDialog.Builder(this)
.setTitle("單選彈窗")
.setIcon(R.mipmap.ic_launcher)
//構(gòu)造參數(shù), 1 數(shù)據(jù)源,2 默認(rèn)被選中的索引,3 條目的點(diǎn)擊事件
.setSingleChoiceItems(R.array.list, 1, new DialogInterface.OnClickListener() {
/**
*
* @param dialog 當(dāng)前的對話框
* @param which 當(dāng)前點(diǎn)擊的是列表的第幾個(gè) item
*/
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "單選彈窗 dialog = " + dialog + "which = " + which, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setPositiveButton("sure", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setCancelable(false)//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
.show();
}
/**
* 創(chuàng)建一個(gè)列表彈窗
*/
private void createListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("列表彈窗");
builder.setItems(R.array.list, new DialogInterface.OnClickListener() {
/**
*
* @param dialog 當(dāng)前的對話框
* @param which 當(dāng)前點(diǎn)擊的是列表的第幾個(gè) item
*/
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "列表 dialog = " + dialog + "which = " + which, Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區(qū)域或者是返回鍵
builder.show();
}
}
列表彈窗:

單選彈窗:

多選彈窗:

日期彈窗:

時(shí)間彈窗:

進(jìn)度條彈窗:

差不多常見的幾種都在這里了,至于還有一個(gè)PopupWindow這里暫時(shí)不作介紹。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio設(shè)置繪制布局時(shí)的視圖
- Android Studio實(shí)現(xiàn)幀動(dòng)畫
- Android Studio實(shí)現(xiàn)補(bǔ)間動(dòng)畫
- Android UI設(shè)計(jì)與開發(fā)之實(shí)現(xiàn)應(yīng)用程序只啟動(dòng)一次引導(dǎo)界面
- Android UI設(shè)計(jì)與開發(fā)之仿人人網(wǎng)V5.9.2最新版引導(dǎo)界面
- Android項(xiàng)目開發(fā)之UI設(shè)計(jì)器
- android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡單計(jì)算器
相關(guān)文章
RecyclerView實(shí)現(xiàn)流式標(biāo)簽單選多選功能
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。這篇文章主要介紹了RecyclerView實(shí)現(xiàn)的流式標(biāo)簽單選多選功能,需要的朋友可以參考下2019-11-11
Android?AMS啟動(dòng)App進(jìn)程原理分析
這篇文章主要介紹了Android?AMS啟動(dòng)App進(jìn)程原理,系統(tǒng)fork函數(shù)是如何創(chuàng)建進(jìn)程,文中有詳細(xì)的代碼示例,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-05-05
Android?Compose之Animatable動(dòng)畫停止使用詳解
這篇文章主要為大家介紹了Android?Compose之Animatable動(dòng)畫停止使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
實(shí)現(xiàn)一個(gè)Android鎖屏App功能的難點(diǎn)總結(jié)
這篇文章主要介紹了實(shí)現(xiàn)一個(gè)Android鎖屏App功能的難點(diǎn)總結(jié),可以有效的解決鎖屏開發(fā)的問題,有需要的可以參考一下。2016-11-11
解決android studio android monitor打不開的問題
下面小編就為大家分享一篇解決android studio android monitor打不開的問題,具有很的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
詳解Android中Application設(shè)置全局變量以及傳值
這篇文章主要介紹了詳解Android中Application設(shè)置全局變量以及傳值的相關(guān)資料,希望通過本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09

