Android開(kāi)發(fā)之對(duì)話框案例詳解(五種對(duì)話框)
下面通過(guò)實(shí)例代碼給大家分享5種android對(duì)話框,具體內(nèi)容詳情如下所示:
1 彈出普通對(duì)話框 --- 系統(tǒng)更新
2 自定義對(duì)話框-- 用戶(hù)登錄
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, "開(kāi)始下載新版本", 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();
}
});
//通過(guò)建造這老構(gòu)建一個(gè)對(duì)話框
Dialog dialog = builder.create();
//顯示
dialog.show();
}
2 自定義對(duì)話框-- 用戶(hù)登錄
布局文件:
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="用戶(hù)名:" />
<EditText android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶(hù)名"/>
<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);
// 獲取用戶(hù)名密碼
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("用戶(hù)名正確");
// 對(duì)話框消失
cus_dialog.dismiss();
}
else{
showToastMsg("用戶(hù)名錯(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開(kāi)發(fā)之對(duì)話框案例詳解(五種對(duì)話框),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android FlowLayout流式布局實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android FlowLayout流式布局的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android入門(mén)之LinearLayout、AbsoluteLayout的用法實(shí)例講解
這篇文章主要介紹了Android入門(mén)之LinearLayout、AbsoluteLayout的用法,對(duì)于Android初學(xué)者有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Android實(shí)現(xiàn)語(yǔ)音識(shí)別代碼
語(yǔ)音識(shí)別在android上使用起來(lái)很方便也很簡(jiǎn)單.但是有個(gè)前提條件,就是android機(jī)器上必須預(yù)先安裝google的語(yǔ)音搜索工具,今天我們就來(lái)詳細(xì)探討下2015-06-06
Android開(kāi)發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)Flutter?桌面應(yīng)用窗口化實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法
這篇文章主要介紹了Android編程中TextView寬度過(guò)大導(dǎo)致Drawable無(wú)法居中問(wèn)題解決方法,以實(shí)例形式較為詳細(xì)的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
初識(shí)Android?PowerManagerService省電模式
這篇文章主要介紹了初識(shí)Android?PowerManagerService省電模式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例
這篇文章主要為大家介紹了Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
android通過(guò)gps獲取定位的位置數(shù)據(jù)和gps經(jīng)緯度
這篇文章主要介紹了android通過(guò)gps獲取定位的位置數(shù)據(jù)示例,大家參考使用吧2014-01-01

