Android對話框使用方法詳解
對話框(Dialog)是Android系統(tǒng)在Activity或者其他組件運行過程中提供的一種提示機制。它可以幫助應用完成一些必要的提示功能,同時提供一些與用戶交互的功能。
對話框分為很多種,下面將一一介紹。
1.提示對話框
Android系統(tǒng)提供的對話框父類為Dialog, 里面并沒有實現(xiàn)對話框的具體類型,比如單選、多選、列表等對話框,僅提供一個框架和規(guī)范。系統(tǒng)為開發(fā)者提供了一個 多功能的對話框類AlertDialog, 里面封裝了各種對話框的樣式,開發(fā)者只須提供相應的顯示數(shù)據(jù)和按鍵的響應監(jiān)聽就可以。
提示對話框所使用的就是系統(tǒng)封裝好的對話框AlertDialog的實例對象。AlertDialog并不提供對外的構(gòu)造方法,即不能直接通過AlertDialog的構(gòu)造函數(shù)來生產(chǎn)一個AlertDialog。因為AlertDialog所有的構(gòu)造方法都是protected的。所以為了獲取AlertDialog對象,系統(tǒng)提供了一個靜態(tài)內(nèi)部類Builder讓我們使用,通過Builder可以創(chuàng)建AlertDialog對象。
(1)創(chuàng)建AlertDialog.Builder實例對象。
(2)通過Builder對象設置對話框的屬性。
- setTitle()設置標題
- setIcon ()設置圖標
- setMessage ()設置要顯示的內(nèi)容
- setItems設置在對話框中顯示的項目列表
- setView設置自定義的對話框樣式
- setPositiveButton ()設置確定按鈕
- setNegativeButton ()設置取消按鈕
- setNeutralButton ()設置中立按鈕
- setSingleChoiceItems單選框
- setMultiChoiceItems復選框
(3)調(diào)用Builder對象的create()方法創(chuàng)建AlertDialog對話框
(4)調(diào)用AlertDialog的show()方法來顯示對話框
(5)調(diào)用AlertDialog的dimiss()方法銷毀對話框。
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("確定要關閉本應用程序嗎?");
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}運行結(jié)果:

2.單選對話框
單選對話框中的0代表默認選中第一個。
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)理", "律師", "公務員"};
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}運行結(jié)果:

3.復選對話框
復選對話框和單選對話框用法相似。
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)理", "律師", "公務員"};
? ? ? ? ? ? ? ? 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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}運行結(jié)果:

4.列表對話框
列表對話框和單選對話框用法相似。
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)理", "律師", "公務員"};
? ? ? ? ? ? ? ? AlertDialog adlg;
? ? ? ? ? ? ? ? adlg=new AlertDialog.Builder(MainActivity.this).setTitle("列表對話框")
? ? ? ? ? ? ? ? ? ? ? ? .setItems(starr, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .create();
? ? ? ? ? ? ? ? adlg.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}運行結(jié)果:

5.進度條對話框
ProgressDialog 也是繼承于Dialog,但其擴展了緩沖加載提示的功能,為人機之間提供了良好的交互體驗。
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("圓形進度條對話框");
? ? ? ? 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("水平進度條對話框");
? ? ? ? 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();
? ? }
}運行結(jié)果:

6.拖動對話框
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("拖動對話框:亮度調(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("當前亮度為:"+progress);
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onStartTrackingTouch(SeekBar seekBar) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onStopTrackingTouch(SeekBar seekBar) {
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? myDlg.show();
? ? }
}運行結(jié)果:

7.日期選擇對話框
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();
? ? }
}運行結(jié)果:

8.時間選擇對話框
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();
? ? }
}運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android自定義有限制區(qū)域圖例角度自識別涂鴉工具類
這篇文章主要為大家介紹了Android自定義有限制區(qū)域圖例角度自識別涂鴉工具類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Android 退出多Activity的application的方式方法
在開發(fā)過程中,我們常常需要一個退出功能,來退出該應用的所有Activity,本篇文章主要介紹了Android 退出多Activity的application的方式,有興趣的可以了解一下。2017-02-02

