AndroidUI控件-AlertDialog彈窗控件
有關android的彈窗界面相信大家見過不少了,手機上很多應用軟件都涉及到彈窗控件,比如典型的每次刪除一個圖片或者卸載一個等都會彈出一個窗口詢問是否刪除/卸載等,還有我們系統的設置時間/日期等,都用到了這樣的控件,下面我將通過代碼來總結下常用的幾個彈窗控件
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="時間彈窗" />
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="進度條彈窗" />
</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;
//表示時間彈窗
private Button mBtn5;
//表示進度條彈窗
private Button mBtn6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
event();
}
/**
* 設置監聽事件
*/
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;
}
}
/**
* 創建一個進度條彈窗
*/
private void createProgressBarDialog() {
//創建進度條彈窗對象
ProgressDialog progressDialog = new ProgressDialog(this);
//設置標題
progressDialog.setTitle("進度條彈窗");
//設置標題圖標
progressDialog.setIcon(R.mipmap.ic_launcher);
//設置文本
progressDialog.setMessage("正在加載...");
//設置最大進度
progressDialog.setMax(100);
//設置進度條的類型
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//顯示進度條彈窗
progressDialog.show();
//如果設置這條語句的話,那麼無論你點擊屏幕外的任何地方或者按返回鍵都取消不了這個彈窗,
//除非在完成進度後,設置取消事件。一般情況這種設置方式對界面很不友好
//不過有時候軟件有重大bug,用戶不得不更新該軟件,如果不更新,就只能
//強制退出程序了
// progressDialog.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區域或者是返回鍵
progressDialog.setProgress(50);
}
/**
* 創建一個日期彈窗
*/
private void createDateDialog() {
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
/**
*
* @param view 當前日期選擇的 view
* @param year 當前選擇的年
* @param monthOfYear 當前選擇的月,從0開始算
* @param dayOfMonth,當前選擇的日,從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)//這裏注意一下的是月份系統表示的是從0開始的,0表示1月,1表示2月.....11表示12月
.show();
}
/**
* 創建一個時間彈窗
*/
private void createTimeDialog() {
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
/**
*
* @param view 當前時間選擇的view
* @param hourOfDay 小時
* @param minute 分鐘
*/
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, "時間彈窗 view = " + view + "hourOfDay = " + hourOfDay + "minute = " + minute, Toast.LENGTH_SHORT).show();
}
}, 11, 22, true)
.show();
}
/**
* 創建一個多選彈窗
*/
private void createMutilDialog() {
new AlertDialog.Builder(this)
.setTitle("多選彈框")
.setIcon(R.mipmap.ic_launcher)
//第二個參數 boolean數組, 如果寫 null 代表默認全部是非選中, 如果想指定某幾個選中,
//需要創建對應長度的數據,按照位置的順序,將指定位置設置爲 true 即可, 數組長度不能小
//於數據源的長度,否則會越界,但是可以大於數據源的長度
.setMultiChoiceItems(R.array.list, new boolean[]{true, false, false, true, false, false, false, false, false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
/**
*
* @param dialog 當前點擊的對話框
* @param which 當前點擊的條目
* @param isChecked 被點擊條目的選中狀態
*/
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(MainActivity.this, "當前點擊的是" + which + " 是否選中" + isChecked, 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)
.show();
}
/**
* 創建一個單選彈窗
*/
private void createSingleDialog() {
new AlertDialog.Builder(this)
.setTitle("單選彈窗")
.setIcon(R.mipmap.ic_launcher)
//構造參數, 1 數據源,2 默認被選中的索引,3 條目的點擊事件
.setSingleChoiceItems(R.array.list, 1, new DialogInterface.OnClickListener() {
/**
*
* @param dialog 當前的對話框
* @param which 當前點擊的是列表的第幾個 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)//不允許被某些方式取消,比如按對話框之外的區域或者是返回鍵
.show();
}
/**
* 創建一個列表彈窗
*/
private void createListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("列表彈窗");
builder.setItems(R.array.list, new DialogInterface.OnClickListener() {
/**
*
* @param dialog 當前的對話框
* @param which 當前點擊的是列表的第幾個 item
*/
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "列表 dialog = " + dialog + "which = " + which, Toast.LENGTH_SHORT).show();
}
});
builder.setCancelable(false);//不允許被某些方式取消,比如按對話框之外的區域或者是返回鍵
builder.show();
}
}
列表彈窗:
單選彈窗:
多選彈窗:
日期彈窗:
時間彈窗:
進度條彈窗:
差不多常見的幾種都在這裏了,至於還有一個PopupWindow這裏暫時不作介紹。