Android對(duì)話框使用方法詳解
對(duì)話框(Dialog)是Android系統(tǒng)在Activity或者其他組件運(yùn)行過程中提供的一種提示機(jī)制。它可以幫助應(yīng)用完成一些必要的提示功能,同時(shí)提供一些與用戶交互的功能。
對(duì)話框分為很多種,下面將一一介紹。
1.提示對(duì)話框
Android系統(tǒng)提供的對(duì)話框父類為Dialog, 里面并沒有實(shí)現(xiàn)對(duì)話框的具體類型,比如單選、多選、列表等對(duì)話框,僅提供一個(gè)框架和規(guī)范。系統(tǒng)為開發(fā)者提供了一個(gè) 多功能的對(duì)話框類AlertDialog, 里面封裝了各種對(duì)話框的樣式,開發(fā)者只須提供相應(yīng)的顯示數(shù)據(jù)和按鍵的響應(yīng)監(jiān)聽就可以。
提示對(duì)話框所使用的就是系統(tǒng)封裝好的對(duì)話框AlertDialog的實(shí)例對(duì)象。AlertDialog并不提供對(duì)外的構(gòu)造方法,即不能直接通過AlertDialog的構(gòu)造函數(shù)來生產(chǎn)一個(gè)AlertDialog。因?yàn)锳lertDialog所有的構(gòu)造方法都是protected的。所以為了獲取AlertDialog對(duì)象,系統(tǒng)提供了一個(gè)靜態(tài)內(nèi)部類Builder讓我們使用,通過Builder可以創(chuàng)建AlertDialog對(duì)象。
(1)創(chuàng)建AlertDialog.Builder實(shí)例對(duì)象。
(2)通過Builder對(duì)象設(shè)置對(duì)話框的屬性。
- setTitle()設(shè)置標(biāo)題
- setIcon ()設(shè)置圖標(biāo)
- setMessage ()設(shè)置要顯示的內(nèi)容
- setItems設(shè)置在對(duì)話框中顯示的項(xiàng)目列表
- setView設(shè)置自定義的對(duì)話框樣式
- setPositiveButton ()設(shè)置確定按鈕
- setNegativeButton ()設(shè)置取消按鈕
- setNeutralButton ()設(shè)置中立按鈕
- setSingleChoiceItems單選框
- setMultiChoiceItems復(fù)選框
(3)調(diào)用Builder對(duì)象的create()方法創(chuàng)建AlertDialog對(duì)話框
(4)調(diào)用AlertDialog的show()方法來顯示對(duì)話框
(5)調(diào)用AlertDialog的dimiss()方法銷毀對(duì)話框。
package com.example.learndialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Button bt= (Button) findViewById(R.id.button); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? AlertDialog aldg; ? ? ? ? ? ? ? ? AlertDialog.Builder adBd=new AlertDialog.Builder(MainActivity.this); ? ? ? ? ? ? ? ? adBd.setTitle("我的提示框"); ? ? ? ? ? ? ? ? adBd.setIcon(R.drawable.p1); ? ? ? ? ? ? ? ? adBd.setMessage("確定要關(guān)閉本應(yīng)用程序嗎?"); ? ? ? ? ? ? ? ? adBd.setPositiveButton("確定", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? finish(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? adBd.setNegativeButton("取消", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? ? ? aldg=adBd.create(); ? ? ? ? ? ? ? ? aldg.show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運(yùn)行結(jié)果:
2.單選對(duì)話框
單選對(duì)話框中的0代表默認(rèn)選中第一個(gè)。
package com.example.learndialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ? ? int picWhich; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Button bt = (Button) findViewById(R.id.button); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? final String[] starr = new String[]{"教師", "經(jīng)理", "律師", "公務(wù)員"}; ? ? ? ? ? ? ? ? AlertDialog adlg; ? ? ? ? ? ? ? ? adlg = new AlertDialog.Builder(MainActivity.this).setTitle("選擇職業(yè):") ? ? ? ? ? ? ? ? ? ? ? ? .setSingleChoiceItems(starr, 0, new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? picWhich = which; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "你選定的職業(yè)是:" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + starr[picWhich], Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .create(); ? ? ? ? ? ? ? ? adlg.show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運(yùn)行結(jié)果:
3.復(fù)選對(duì)話框
復(fù)選對(duì)話框和單選對(duì)話框用法相似。
package com.example.learndialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ? ? int picWhich; ? ? boolean chk[]=new boolean[]{false,false,false,false}; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Button bt = (Button) findViewById(R.id.button); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? final String[] starr = new String[]{"教師", "經(jīng)理", "律師", "公務(wù)員"}; ? ? ? ? ? ? ? ? AlertDialog adlg; ? ? ? ? ? ? ? ? adlg = new AlertDialog.Builder(MainActivity.this).setTitle("選擇職業(yè):") ? ? ? ? ? ? ? ? ? ? ? ? .setMultiChoiceItems(starr, chk, new DialogInterface.OnMultiChoiceClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which, boolean isChecked) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? chk[which]=isChecked; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("確定", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String st=" "; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i=0;i<chk.length;i++) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chk[i]) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? st=st+starr[i]+" "; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"選定的職業(yè)有:"+st,Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .create(); ? ? ? ? ? ? ? ? adlg.show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運(yùn)行結(jié)果:
4.列表對(duì)話框
列表對(duì)話框和單選對(duì)話框用法相似。
package com.example.learndialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Button bt = (Button) findViewById(R.id.button); ? ? ? ? bt.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? final String[] starr = new String[]{"教師", "經(jīng)理", "律師", "公務(wù)員"}; ? ? ? ? ? ? ? ? AlertDialog adlg; ? ? ? ? ? ? ? ? adlg=new AlertDialog.Builder(MainActivity.this).setTitle("列表對(duì)話框") ? ? ? ? ? ? ? ? ? ? ? ? .setItems(starr, new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? ? ? ? ? .create(); ? ? ? ? ? ? ? ? adlg.show(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
運(yùn)行結(jié)果:
5.進(jìn)度條對(duì)話框
ProgressDialog 也是繼承于Dialog,但其擴(kuò)展了緩沖加載提示的功能,為人機(jī)之間提供了良好的交互體驗(yàn)。
package com.example.learndialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? } ? ? public void progress_circle(View v){ ? ? ? ? final ProgressDialog prdg1=new ProgressDialog(MainActivity.this); ? ? ? ? prdg1.setProgressStyle(ProgressDialog.STYLE_SPINNER); ? ? ? ? prdg1.setTitle("圓形進(jìn)度條對(duì)話框"); ? ? ? ? prdg1.setMessage("正在下載"); ? ? ? ? prdg1.setMax(100); ? ? ? ? prdg1.show(); ? ? ? ? new Thread(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? Thread.sleep(5000); ? ? ? ? ? ? ? ? ? ? prdg1.cancel(); ? ? ? ? ? ? ? ? } catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }).start(); ? ? } ? ? public void progress_horizontal(View v){ ? ? ? ? final ProgressDialog prdg2 = new ProgressDialog(this); ? ? ? ? prdg2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); ? ? ? ? prdg2.setCancelable(true); ? ? ? ? prdg2.setTitle("水平進(jìn)度條對(duì)話框"); ? ? ? ? prdg2.setMax(100); ? ? ? ? prdg2.setButton(DialogInterface.BUTTON_POSITIVE, "確定", ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? prdg2.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", ? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? prdg2.setMessage("正在下載"); ? ? ? ? prdg2.show(); ? ? ? ? new Thread(new Runnable() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void run() { ? ? ? ? ? ? ? ? int i = 0; ? ? ? ? ? ? ? ? while (i < 100) { ? ? ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(100); ? ? ? ? ? ? ? ? ? ? ? ? prdg2.incrementProgressBy(1); ? ? ? ? ? ? ? ? ? ? ? ? i++; ? ? ? ? ? ? ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? prdg2.dismiss(); ? ? ? ? ? ? } ? ? ? ? }).start(); ? ? } }
運(yùn)行結(jié)果:
6.拖動(dòng)對(duì)話框
package com.example.learndialog; import android.app.Dialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? } ? ? public void SeekOnClick(View v){ ? ? ? ? Dialog myDlg=new Dialog(MainActivity.this); ? ? ? ? myDlg.setTitle("拖動(dòng)對(duì)話框:亮度調(diào)節(jié)"); ? ? ? ? myDlg.setContentView(R.layout.seekbardlg); ? ? ? ? SeekBar sb=(SeekBar)myDlg.findViewById(R.id.seekBar); ? ? ? ? final TextView tv=(TextView)myDlg.findViewById(R.id.textView); ? ? ? ? sb.setMax(100); ? ? ? ? sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ? ? ? ? ? ? ? ? tv.setText("當(dāng)前亮度為:"+progress); ? ? ? ? ? ? } ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onStartTrackingTouch(SeekBar seekBar) { ? ? ? ? ? ? } ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onStopTrackingTouch(SeekBar seekBar) { ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? myDlg.show(); ? ? } }
運(yùn)行結(jié)果:
7.日期選擇對(duì)話框
package com.example.datepickerdialog; import android.app.DatePickerDialog; import android.icu.util.Calendar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? Calendar c=Calendar.getInstance(); ? ? ? ? DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onDateSet(DatePicker view, int year, int month, int day) { ? ? ? ? ? ? ? ? String st; ? ? ? ? ? ? ? ? st = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day); ? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG).show(); ? ? ? ? ? ? } ? ? ? ? },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)); ? ? ? ? dpd.show(); ? ? } }
運(yùn)行結(jié)果:
8.時(shí)間選擇對(duì)話框
package com.example.datepickerdialog; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.icu.util.Calendar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ?Calendar calendar=Calendar.getInstance(); ? ? ? ? new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onTimeSet(TimePicker view, int hourOfDay, int minute) { ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? ? ? ? ? calendar.get(Calendar.HOUR_OF_DAY), ? ? ? ? ? ? ? ? calendar.get(Calendar.MINUTE), ? ? ? ? ? ? ? ? true ? ? ? ? ).show(); ? ? } }
運(yùn)行結(jié)果:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android入門之彈出式對(duì)話框的實(shí)現(xiàn)
- Android入門之利用Spinner實(shí)現(xiàn)彈出選擇對(duì)話框
- Android自定義對(duì)話框的簡單實(shí)現(xiàn)
- 五分了解Android?Progress?Bar進(jìn)度條加載
- Android開發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例
- Android自定義View實(shí)現(xiàn)進(jìn)度條動(dòng)畫
- Android實(shí)現(xiàn)簡單實(shí)用的垂直進(jìn)度條
- android實(shí)現(xiàn)簡單進(jìn)度條ProgressBar效果
- Jetpack Compose實(shí)現(xiàn)對(duì)話框和進(jìn)度條實(shí)例解析
相關(guān)文章
Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類
這篇文章主要為大家介紹了Android自定義有限制區(qū)域圖例角度自識(shí)別涂鴉工具類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02超實(shí)用的Android手勢鎖制作實(shí)例教程
這篇文章主要介紹了一個(gè)超實(shí)用的Android手勢鎖制作實(shí)例教程,普通的圓環(huán)形圖標(biāo)變換,在App和系統(tǒng)的鎖屏界面中都可以調(diào)用,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)橫豎屏切換的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Android studio配置lambda表達(dá)式教程
Java 8的一個(gè)大亮點(diǎn)是引入Lambda表達(dá)式,使用它設(shè)計(jì)的代碼會(huì)更加簡潔。接下來通過本文給大家介紹Android studio配置lambda表達(dá)式教程,需要的朋友參考下吧2017-05-05Android 退出多Activity的application的方式方法
在開發(fā)過程中,我們常常需要一個(gè)退出功能,來退出該應(yīng)用的所有Activity,本篇文章主要介紹了Android 退出多Activity的application的方式,有興趣的可以了解一下。2017-02-02Android使用Volley實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了Android使用Volley實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12