詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
ProgressDialog(精度條對(duì)話框):
1.直接調(diào)用ProgressDialog提供的靜態(tài)方法show()顯示
2.創(chuàng)建ProgressDialog,再設(shè)置對(duì)話框的參數(shù),最后show()出來
package com.example.test3; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ private Button btn_one; private Button btn_two; private Button btn_three; private ProgressDialog pd1 = null; private ProgressDialog pd2 = null; private final static int MAXVALUE = 100; private int progressStart = 0; private int add = 0; private Context mContext = null; //定義一個(gè)用于更新進(jìn)度的Handler,因?yàn)橹荒苡芍骶€程更新界面,所以要用Handler傳遞信息 final Handler hand = new Handler() { @Override public void handleMessage(Message msg) { //這里的話如果接受到信息碼是123 if(msg.what == 123) { //設(shè)置進(jìn)度條的當(dāng)前值 pd2.setProgress(progressStart); } //如果當(dāng)前大于或等于進(jìn)度條的最大值,調(diào)用dismiss()方法關(guān)閉對(duì)話框 if(progressStart >= MAXVALUE) { pd2.dismiss(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; bindViews(); } private void bindViews() { btn_one = (Button) findViewById(R.id.btn1); btn_two = (Button) findViewById(R.id.btn2); btn_three = (Button) findViewById(R.id.btn3); btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn1: //這里的話參數(shù)依次為,上下文,標(biāo)題,內(nèi)容,是否顯示進(jìn)度,是否可以用取消按鈕關(guān)閉 ProgressDialog.show(MainActivity.this, "資源加載中", "資源加載中,請(qǐng)稍后...",false,true); break; case R.id.btn2: pd1 = new ProgressDialog(mContext); //依次設(shè)置標(biāo)題,內(nèi)容,是否用取消按鈕關(guān)閉,是否顯示進(jìn)度 pd1.setTitle("軟件更新中"); pd1.setMessage("軟件正在更新中,請(qǐng)稍后..."); pd1.setCancelable(true); //這里是設(shè)置進(jìn)度條的風(fēng)格,HORIZONTAL是水平進(jìn)度條,SPINNER是圓形進(jìn)度條 pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd1.setIndeterminate(true); //調(diào)用show()方法將ProgressDialog顯示出來 pd1.show(); break; case R.id.btn3: //初始化屬性 progressStart = 0; add = 0; //依次設(shè)置一些屬性 pd2 = new ProgressDialog(MainActivity.this); pd2.setMax(MAXVALUE); pd2.setTitle("文件讀取中"); pd2.setMessage("文件加載中,請(qǐng)稍后..."); //這里設(shè)置為不可以通過按取消按鈕關(guān)閉進(jìn)度條 pd2.setCancelable(false); pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //這里設(shè)置的是是否顯示進(jìn)度,設(shè)為false才是顯示的哦! pd2.setIndeterminate(false); pd2.show(); //這里的話新建一個(gè)線程,重寫run()方法, new Thread() { public void run() { while(progressStart < MAXVALUE) { //這里的算法是決定進(jìn)度條變化的,可以按需要寫 progressStart = 2 * usetime() ; //把信息碼發(fā)送給handle讓更新界面 hand.sendEmptyMessage(123); } } }.start(); break; } } //這里設(shè)置一個(gè)耗時(shí)的方法: private int usetime() { add++; try{ Thread.sleep(100); }catch (InterruptedException e) { e.printStackTrace(); } return add; } }
2.Date/TimePickerDialog只是供用戶來選擇日期時(shí)間,對(duì)于android系統(tǒng)的系統(tǒng)時(shí)間, 日期沒有任何影響
他們兩個(gè)的構(gòu)造方法非常相似: DatePickerDialog(上下文;DatePickerDialog.OnDateSetListener()監(jiān)聽器;年;月;日)
TimePickerDialog(上下文;TimePickerDialog.OnTimeSetListener()監(jiān)聽器;小時(shí),分鐘,是否采用24小時(shí)制)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_date; private Button btn_time; private String result = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); } private void bindViews() { btn_date = (Button) findViewById(R.id.btn_date); btn_time = (Button) findViewById(R.id.btn_time); btn_date.setOnClickListener(this); btn_time.setOnClickListener(this); } @Override public void onClick(View v) { result = ""; switch (v.getId()){ case R.id.btn_date: Calendar cale1 = Calendar.getInstance(); new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //這里獲取到的月份需要加上1哦~ result += "你選擇的是"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show(); } } ,cale1.get(Calendar.YEAR) ,cale1.get(Calendar.MONTH) ,cale1.get(Calendar.DAY_OF_MONTH)).show(); break; case R.id.btn_time: Calendar cale2 = Calendar.getInstance(); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { result = ""; result += "您選擇的時(shí)間是:"+hourOfDay+"時(shí)"+minute+"分"; Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show(); } }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show(); break; } } }
最后一個(gè)用于顯示信息的UI控件——PopupWindow(懸浮框),如果你想知道 他長(zhǎng)什么樣子,你可以打開你手機(jī)的QQ,長(zhǎng)按列表中的某項(xiàng),這個(gè)時(shí)候后彈出一個(gè)黑色的小 對(duì)話框,這種就是PopupWindow了,和AlertDialog對(duì)話框不同的是,他的位置可以是隨意的; 另外AlertDialog是非堵塞線程的,而PopupWindow則是堵塞線程的
1)幾個(gè)常用的構(gòu)造方法
我們?cè)谖臋n中可以看到,提供給我們的PopupWindow的構(gòu)造方法有九種之多,這里只貼實(shí)際 開發(fā)中用得較多的幾個(gè)構(gòu)造方法:
public PopupWindow (Context context) public PopupWindow(View contentView, int width, int height) public PopupWindow(View contentView) public PopupWindow(View contentView, int width, int height, boolean focusable)
參數(shù)就不用多解釋了吧,contentView是PopupWindow顯示的View,focusable是否顯示焦點(diǎn)
2)常用的一些方法
下面介紹幾個(gè)用得較多的一些方法,其他的可自行查閱文檔:
setContentView(View contentView):設(shè)置PopupWindow顯示的View
getContentView():獲得PopupWindow顯示的View
showAsDropDown(View anchor):相對(duì)某個(gè)控件的位置(正左下方),無偏移
showAsDropDown(View anchor, int xoff, int yoff):相對(duì)某個(gè)控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y): 相對(duì)于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設(shè)置偏移或無偏移 PS:parent這個(gè)參數(shù)只要是activity中的view就可以了!
setWidth/setHeight:設(shè)置寬高,也可以在構(gòu)造方法那里指定好寬高, 除了可以寫具體的值,還可以用WRAP_CONTENT或MATCH_PARENT, popupWindow的width和height屬性直接和第一層View相對(duì)應(yīng)。
setFocusable(true):設(shè)置焦點(diǎn),PopupWindow彈出后,所有的觸屏和物理按鍵都由PopupWindows 處理。其他任何事件的響應(yīng)都必須發(fā)生在PopupWindow消失之后,(home 等系統(tǒng)層面的事件除外)。 比如這樣一個(gè)PopupWindow出現(xiàn)的時(shí)候,按back鍵首先是讓PopupWindow消失,第二次按才是退出 activity,準(zhǔn)確的說是想退出activity你得首先讓PopupWindow消失,因?yàn)椴徊⑹侨魏吻闆r下按back PopupWindow都會(huì)消失,必須在PopupWindow設(shè)置了背景的情況下 。
setAnimationStyle(int):設(shè)置動(dòng)畫效果
public class MainActivity extends Activity { private Button btn_show; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; btn_show = (Button) findViewById(R.id.btn_show); btn_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { initPopWindow(v); } }); } private void initPopWindow(View v) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false); Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi); Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe); //1.構(gòu)造一個(gè)PopupWindow,參數(shù)依次是加載的View,寬高 final PopupWindow popWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popWindow.setAnimationStyle(R.anim.anim_pop); //設(shè)置加載動(dòng)畫 //這些為了點(diǎn)擊非PopupWindow區(qū)域,PopupWindow會(huì)消失的,如果沒有下面的 //代碼的話,你會(huì)發(fā)現(xiàn),當(dāng)你把PopupWindow顯示出來了,無論你按多少次后退鍵 //PopupWindow并不會(huì)關(guān)閉,而且退不出程序,加上下述代碼可以解決這個(gè)問題 popWindow.setTouchable(true); popWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; // 這里如果返回true的話,touch事件將被攔截 // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點(diǎn)擊外部區(qū)域無法dismiss } }); popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); //要為popWindow設(shè)置一個(gè)背景才有效 //設(shè)置popupWindow顯示的位置,參數(shù)依次是參照View,x軸的偏移量,y軸的偏移量 popWindow.showAsDropDown(v, 50, 0); //設(shè)置popupWindow里的按鈕的事件 btn_xixi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點(diǎn)擊了嘻嘻~", Toast.LENGTH_SHORT).show(); } }); btn_hehe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "你點(diǎn)擊了呵呵~", Toast.LENGTH_SHORT).show(); popWindow.dismiss(); } }); } }
相關(guān)文章
詳解Android .9.png “點(diǎn)九”圖片的使用
這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下2016-09-09Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程,分別介紹了進(jìn)度條和圖片上傳并排列的例子,效果很好很強(qiáng)大,需要的朋友可以參考下2016-05-05Jetpack?Compose?的新型架構(gòu)?MVI使用詳解
這篇文章主要介紹了Jetpack?Compose?的新型架構(gòu)?MVI使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android在listview添加checkbox實(shí)現(xiàn)原理與代碼
Android在listview添加checkbox如何實(shí)現(xiàn)一直都是新手朋友們的頭疼問題,接下來為您詳細(xì)介紹實(shí)現(xiàn)方法,感興趣的朋友可以了解下2013-01-01Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼
EditText搜索結(jié)果下拉框、自動(dòng)or回調(diào)模式、可diy、使用超簡(jiǎn)便。這篇文章主要介紹了Android BSearchEdit 搜索結(jié)果選擇框的實(shí)例代碼,需要的朋友可以參考下2019-10-10Android自定義View實(shí)現(xiàn)閃耀字體效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)閃耀字體效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android判斷是否有拍照權(quán)限的實(shí)例代碼
android在開發(fā)中有時(shí)候要判斷應(yīng)用中是否有某項(xiàng)權(quán)限,下面通過本文給大家分享Android判斷是否有拍照權(quán)限的實(shí)例代碼,需要的的朋友參考下吧2017-07-07