Android開發(fā)之對話框案例詳解(五種對話框)
下面通過實例代碼給大家分享5種android對話框,具體內(nèi)容詳情如下所示:
1 彈出普通對話框 --- 系統(tǒng)更新
2 自定義對話框-- 用戶登錄
3 時間選擇對話框 -- 時間對話框
4 進度條對話框 -- 信息加載..
5 popuWindow對話框
1 彈出普通對話框 --- 系統(tǒng)更新
//彈出普通對話框 public void showNormalDialog(View v) { AlertDialog.Builder builder = new Builder(this); //設(shè)置Dialog的圖標 builder.setIcon(R.drawable.ic_launcher); //設(shè)置對話框的標題 builder.setTitle("更新"); //設(shè)置message builder.setMessage("發(fā)現(xiàn)新版本是否更新?"); //確定按鈕 取消按鈕 builder.setPositiveButton("確定",new OnClickListener() { /** * 點擊確定按鈕 回調(diào)該方法 */ @Override public void onClick(DialogInterface dialog, int which) { //到服務(wù)器去下載新的版本 duration單詞意思:時長 Toast.makeText(MainActivity.this, "開始下載新版本", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { /** * 點擊取消按鈕 回調(diào)該方法 */ @Override public void onClick(DialogInterface dialog, int which) { //到服務(wù)器去下載新的版本 duration單詞意思:時長 Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("下一次", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //到服務(wù)器去下載新的版本 duration單詞意思:時長 Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show(); } }); //通過建造這老構(gòu)建一個對話框 Dialog dialog = builder.create(); //顯示 dialog.show(); }
2 自定義對話框-- 用戶登錄
布局文件:
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="請輸入用戶名"/> <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="請輸入密碼"/> <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代碼:
//自定義對話框 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è)置自定義的對話框界面 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 自動生成的方法存根 if(name.getText().toString().trim().equals("abc")){ showToastMsg("用戶名正確"); // 對話框消失 cus_dialog.dismiss(); } else{ showToastMsg("用戶名錯誤"); } } }); Button btnCancel = (Button) view.findViewById(R.id.btn_cancel); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 對話框消失 cus_dialog.dismiss(); } }); cus_dialog = builder.create(); cus_dialog.show(); }
下載地址:http://www.jinhusns.com/Products/Download/?type=yhq
3 時間選擇對話框 -- 時間對話框
// 時間選擇對話框 public void showTimePickerDialog(View v){ Calendar sysDate = Calendar.getInstance(); //設(shè)置系統(tǒng)時間 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 自動生成的方法存根 showToastMsg(" hourOfDay:" + hourOfDay + " minute:" + minute); } }, //callBack 選擇時間后的回調(diào)方法 hour,//hourOfDay 當(dāng)前系統(tǒng)時間 minute,//hourOfDay 當(dāng)前系統(tǒng)時間 true);//是否24小時制 time.show(); }
4 進度條對話框 -- 信息加載..
/** * 進度條對話框 * @param v */ public void showProgressDialog(View v){ final ProgressDialog progress = new ProgressDialog(this); progress.setProgress(R.drawable.img2); progress.setTitle("標題"); progress.setMessage("加載中..."); //樣式1 進度條樣式 progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //樣式2 有加載圖標 //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 自動生成的方法存根 showToastMsg("確定按鈕"); } }); progress.setButton2("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自動生成的方法存根 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); // 更新進度條的進度,可以在子線程中更新進度條進度 progress.incrementProgressBy(5); // progress.incrementSecondaryProgressBy(10);//二級進度條更新方式 i += 5; } catch (Exception e) { // TODO: handle exception } } // 在進度條走完時刪除Dialog progress.dismiss(); } }).start(); }
5 popuWindow對話框
Button btn_popu; //popuWindow對話框 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]; // 獲取按鈕坐標 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 自動生成的方法存根 showToastMsg("點擊了啟動"); } }); }
總結(jié)
以上所述是小編給大家介紹的詳解Android開發(fā)之對話框案例詳解(五種對話框),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android自定義View實現(xiàn)shape圖形繪制
這篇文章主要為大家詳細介紹了Android使用自定義View實現(xiàn)shape圖形繪制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android編程開發(fā)之NotiFication用法詳解
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結(jié)合實例形式較為詳細的分析了NotiFication的功能、使用技巧與注意事項,需要的朋友可以參考下2015-12-12Android實現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型(三)
這篇文章主要為大家詳細介紹了Android實現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android自定義view實現(xiàn)側(cè)滑欄詳解
之前一直沒有寫側(cè)滑菜單的實現(xiàn)方法,今天計劃補上。手機開發(fā)中,往往存在很多功能沒處放的問題。我們可能會把功能放入一個菜單列表,但現(xiàn)在一種流行的做法是側(cè)滑菜單2022-11-11Android中EditText實現(xiàn)不可編輯解決辦法
這篇文章主要介紹了Android中EditText實現(xiàn)不可編輯解決辦法,需要的朋友可以參考下2014-12-12Android PopupWindow使用方法小結(jié)
這篇文章主要為大家詳細介紹了Android PopupWindow使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06