国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發之對話框案例詳解(五種對話框)

2019-10-22 18:27:57
字體:
來源:轉載
供稿:網友

下面通過實例代碼給大家分享5種android對話框,具體內容詳情如下所示:

1 彈出普通對話框 --- 系統更新

2 自定義對話框-- 用戶登錄

3 時間選擇對話框 -- 時間對話框

4 進度條對話框 -- 信息加載..

5 popuWindow對話框

1 彈出普通對話框 --- 系統更新 

//彈出普通對話框    public void showNormalDialog(View v) {     AlertDialog.Builder builder = new Builder(this);     //設置Dialog的圖標     builder.setIcon(R.drawable.ic_launcher);     //設置對話框的標題     builder.setTitle("更新");     //設置message     builder.setMessage("發現新版本是否更新?");     //確定按鈕  取消按鈕     builder.setPositiveButton("確定",new OnClickListener() {       /**        * 點擊確定按鈕 回調該方法        */       @Override       public void onClick(DialogInterface dialog, int which) {         //到服務器去下載新的版本 duration單詞意思:時長         Toast.makeText(MainActivity.this, "開始下載新版本", Toast.LENGTH_SHORT).show();       }     });     builder.setNegativeButton("取消", new OnClickListener() {       /**        * 點擊取消按鈕 回調該方法        */       @Override       public void onClick(DialogInterface dialog, int which) {         //到服務器去下載新的版本 duration單詞意思:時長         Toast.makeText(MainActivity.this, "不需要更新", Toast.LENGTH_SHORT).show();       }     });     builder.setNeutralButton("下一次", new OnClickListener() {       @Override       public void onClick(DialogInterface dialog, int which) {         //到服務器去下載新的版本 duration單詞意思:時長         Toast.makeText(MainActivity.this, "下一次吧", Toast.LENGTH_SHORT).show();       }     });     //通過建造這老構建一個對話框     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);     // 設置自定義的對話框界面     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();   //設置系統時間   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 選擇時間后的回調方法       hour,//hourOfDay 當前系統時間       minute,//hourOfDay 當前系統時間       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);     //當前     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;     // 設置布局     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("點擊了啟動");       }     });   } 

總結

以上所述是小編給大家介紹的詳解Android開發之對話框案例詳解(五種對話框),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大英县| 渝北区| 芒康县| 巴彦县| 合江县| 容城县| 陆良县| 福清市| 丹东市| 洞头县| 巩义市| 定兴县| 崇信县| 娄烦县| 稻城县| 中方县| 南安市| 灯塔市| 郴州市| 长治县| 广宁县| 盈江县| 丹阳市| 大荔县| 北宁市| 赞皇县| 定南县| 紫金县| 上林县| 灌阳县| 安溪县| 股票| 千阳县| 安康市| 南宁市| 绥德县| 大兴区| 兴化市| 柳河县| 温州市| 故城县|