Android開發(fā)之對(duì)話框案例詳解(五種對(duì)話框)
下面通過實(shí)例代碼給大家分享5種android對(duì)話框,具體內(nèi)容詳情如下所示:
1 彈出普通對(duì)話框 --- 系統(tǒng)更新
2 自定義對(duì)話框-- 用戶登錄
3 時(shí)間選擇對(duì)話框 -- 時(shí)間對(duì)話框
4 進(jìn)度條對(duì)話框 -- 信息加載..
5 popuWindow對(duì)話框
1 彈出普通對(duì)話框 --- 系統(tǒng)更新
//彈出普通對(duì)話框
public void showNormalDialog(View v) {
AlertDialog.Builder builder = new Builder(this);
//設(shè)置Dialog的圖標(biāo)
builder.setIcon(R.drawable.ic_launcher);
//設(shè)置對(duì)話框的標(biāo)題
builder.setTitle("更新");
//設(shè)置message
builder.setMessage("發(fā)現(xiàn)新版本是否更新?");
//確定按鈕 取消按鈕
builder.setPositiveButton("確定",new OnClickListener() {
/**
* 點(diǎn)擊確定按鈕 回調(diào)該方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務(wù)器去下載新的版本 duration單詞意思:時(shí)長(zhǎng)
Toast.makeText(MainActivity.this, "開始下載新版本", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
/**
* 點(diǎn)擊取消按鈕 回調(diào)該方法
*/
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務(wù)器去下載新的版本 duration單詞意思:時(shí)長(zhǎng)
Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("下一次", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//到服務(wù)器去下載新的版本 duration單詞意思:時(shí)長(zhǎng)
Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();
}
});
//通過建造這老構(gòu)建一個(gè)對(duì)話框
Dialog dialog = builder.create();
//顯示
dialog.show();
}
2 自定義對(duì)話框-- 用戶登錄
布局文件:
user_name_dialog.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:padding="10dip"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄信息"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名:" />
<EditText android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶名"/>
<TextView
android:id="@+id/tv_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼:" />
<EditText android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請(qǐng)輸入密碼"/>
<requestFocus />
<Button
android:id="@+id/btn_confirm"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登錄" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="取消" />
</LinearLayout>
java代碼:
//自定義對(duì)話框
Dialog cus_dialog ;
public void showCustomDialog(View v){
AlertDialog.Builder builder = new Builder(this);
// 布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.user_name_dialog, null);
// 設(shè)置自定義的對(duì)話框界面
builder.setView(view);
// 獲取用戶名密碼
final EditText name = (EditText) view.findViewById(R.id.et_name);
final EditText pwd = (EditText) view.findViewById(R.id.et_pwd);
Button btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自動(dòng)生成的方法存根
if(name.getText().toString().trim().equals("abc")){
showToastMsg("用戶名正確");
// 對(duì)話框消失
cus_dialog.dismiss();
}
else{
showToastMsg("用戶名錯(cuò)誤");
}
}
});
Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 對(duì)話框消失
cus_dialog.dismiss();
}
});
cus_dialog = builder.create();
cus_dialog.show();
}
下載地址:http://www.jinhusns.com/Products/Download/?type=yhq
3 時(shí)間選擇對(duì)話框 -- 時(shí)間對(duì)話框
// 時(shí)間選擇對(duì)話框
public void showTimePickerDialog(View v){
Calendar sysDate = Calendar.getInstance();
//設(shè)置系統(tǒng)時(shí)間
sysDate.setTimeInMillis(System.currentTimeMillis());
int hour = sysDate.get(Calendar.HOUR_OF_DAY);
int minute = sysDate.get(Calendar.MINUTE);
TimePickerDialog time = new TimePickerDialog(this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO 自動(dòng)生成的方法存根
showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute);
}
}, //callBack 選擇時(shí)間后的回調(diào)方法
hour,//hourOfDay 當(dāng)前系統(tǒng)時(shí)間
minute,//hourOfDay 當(dāng)前系統(tǒng)時(shí)間
true);//是否24小時(shí)制
time.show();
}
4 進(jìn)度條對(duì)話框 -- 信息加載..
/**
* 進(jìn)度條對(duì)話框
* @param v
*/
public void showProgressDialog(View v){
final ProgressDialog progress = new ProgressDialog(this);
progress.setProgress(R.drawable.img2);
progress.setTitle("標(biāo)題");
progress.setMessage("加載中...");
//樣式1 進(jìn)度條樣式
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//樣式2 有加載圖標(biāo)
//progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//最大
progress.setMax(100);
//當(dāng)前
progress.setProgress(50);
progress.setButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自動(dòng)生成的方法存根
showToastMsg("確定按鈕");
}
});
progress.setButton2("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自動(dòng)生成的方法存根
showToastMsg("取消按鈕");
}
});
progress.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新進(jìn)度條的進(jìn)度,可以在子線程中更新進(jìn)度條進(jìn)度
progress.incrementProgressBy(5);
// progress.incrementSecondaryProgressBy(10);//二級(jí)進(jìn)度條更新方式
i += 5;
} catch (Exception e) {
// TODO: handle exception
}
}
// 在進(jìn)度條走完時(shí)刪除Dialog
progress.dismiss();
}
}).start();
}
5 popuWindow對(duì)話框
Button btn_popu;
//popuWindow對(duì)話框
public void showPopuWindow(View v){
btn_popu = (Button) v;
// 設(shè)置布局
View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
PopupWindow window = new PopupWindow(this);
window.setContentView(view);
window.setWidth(360);
window.setHeight(200);
int[] location = new int[2];
// 獲取按鈕坐標(biāo)
btn_popu.getLocationInWindow(location);
window.setFocusable(true);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.back_null));
window.showAtLocation(btn_popu, Gravity.LEFT |Gravity.TOP , location[0]+ btn_popu.getWidth(), location[1] + 0 );
//showToastMsg("" + (location[0]+ btn_popu.getWidth())+" "+ (location[1] + btn_popu.getHeight() / 2));
ImageView img_start = (ImageView) view.findViewById(R.id.img_start);
img_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自動(dòng)生成的方法存根
showToastMsg("點(diǎn)擊了啟動(dòng)");
}
});
}
總結(jié)
以上所述是小編給大家介紹的詳解Android開發(fā)之對(duì)話框案例詳解(五種對(duì)話框),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自定義View實(shí)現(xiàn)shape圖形繪制
這篇文章主要為大家詳細(xì)介紹了Android使用自定義View實(shí)現(xiàn)shape圖形繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android編程開發(fā)之NotiFication用法詳解
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了NotiFication的功能、使用技巧與注意事項(xiàng),需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型(三)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android自定義view實(shí)現(xiàn)側(cè)滑欄詳解
之前一直沒有寫側(cè)滑菜單的實(shí)現(xiàn)方法,今天計(jì)劃補(bǔ)上。手機(jī)開發(fā)中,往往存在很多功能沒處放的問題。我們可能會(huì)把功能放入一個(gè)菜單列表,但現(xiàn)在一種流行的做法是側(cè)滑菜單2022-11-11
Android中EditText實(shí)現(xiàn)不可編輯解決辦法
這篇文章主要介紹了Android中EditText實(shí)現(xiàn)不可編輯解決辦法,需要的朋友可以參考下2014-12-12
Android實(shí)現(xiàn)計(jì)步傳感器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)步傳感器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Android 實(shí)現(xiàn)仿支付寶的密碼均分輸入框
這篇文章主要介紹了Android 實(shí)現(xiàn)仿支付寶的密碼均分輸入框的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

