生活随笔
收集整理的這篇文章主要介紹了
【Android】对话框 AlertDialog
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
【Android】對話框 AlertDialog
本講介紹一下Android基本組件:對話框AlertDialog。
?來源:http://blog.csdn.net/feng88724/article/details/6171450
API:
| java.lang.Object |
| ???? | android.app.AlertDialog.Builder |
?
使用AlertDialog.Builder創(chuàng)建對話框需要了解以下幾個方法:
- setTitle :為對話框設(shè)置標(biāo)題
- setIcon :為對話框設(shè)置圖標(biāo)
- setMessage:為對話框設(shè)置內(nèi)容
- setView : 給對話框設(shè)置自定義樣式
- setItems :設(shè)置對話框要顯示的一個list,一般用于顯示幾個命令時。
- setMultiChoiceItems :用來設(shè)置對話框顯示一系列的復(fù)選框。
- setNeutralButton ? ?:
- setPositiveButton ? :給對話框添加"Yes"按鈕
- setNegativeButton :對話框添加"No"按鈕
- create : 創(chuàng)建對話框
- show :顯示對話框
?
下面我們來看一下最簡單對話框。
?
這個對話框只是簡單的顯示內(nèi)容,使用默認圖標(biāo),沒有按鈕,不多說,貼代碼:
[java]?view plaincopyprint?
new?AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示標(biāo)題").setMessage("這是提示內(nèi)容").show();??? ?
(Lesson_02_Dia是類名,請換成自己的!!)
?
?
下面我們?yōu)檫@個對話框加個按鈕,效果:
?
?
代碼:
[java]?view plaincopyprint?
new?AlertDialog.Builder(Lesson_01_Pic.this)?? ?????????.setTitle("這是標(biāo)題")?? ????????.setMessage("這是提示內(nèi)容")??? ????????.setPositiveButton("確定",??? ????????new?DialogInterface.OnClickListener(){?? ??????????????????public?void?onClick(DialogInterface?dialoginterface,?int?i){??? ??????????????????????????????????? ????????????????????????????Toast.makeText(Lesson_01_Pic.this,?"確定",Toast.LENGTH_LONG).show();?? ??????????????????????????????}??? ??????????????????????}).show();??? ?
?
添加按鈕時,需要同時為該按鈕指定監(jiān)聽器。
?
?
下面,我們修改一個圖標(biāo),添加兩個按鈕,同時顯示多個選項,先看下效果:
?
代碼:
[c-sharp]?view plaincopyprint?
package?com.yfz;?? import?android.app.Activity;?? import?android.app.AlertDialog;?? import?android.app.Dialog;?? import?android.content.DialogInterface;?? import?android.content.DialogInterface.OnClickListener;?? import?android.content.DialogInterface.OnMultiChoiceClickListener;?? import?android.os.Bundle;?? import?android.view.View;?? import?android.widget.Button;?? import?android.widget.Toast;?? public?class?Lesson_02_Dia?extends?Activity?{?? ?????? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.main);?? ?????????? ????????Button?button?=?(Button)findViewById(R.id.b01);?? ????????button.setText("對話框");?? ????????button.setOnClickListener(new?Button.OnClickListener(){?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ?????????????????? ????????????????String[]?choices={"Facebook","Twitter"};?? ?????????????????? ????????????????boolean[]?chsBool?=?{true,false};?? ??????????????????? ????????????????AlertDialog?dialog?=?new?AlertDialog.Builder(Lesson_02_Dia.this)?? ?????????????????????????.setIcon(android.R.drawable.btn_star_big_on)?? ?????????????????????????.setTitle("調(diào)查")?? ?????????????????????????.setMultiChoiceItems(choices,?chsBool,?multiClick)?? ?????????????????????????.setPositiveButton("Yes",?onclick)?? ?????????????????????????.setNegativeButton("No",??onclick).create();?? ????????????????dialog.show();?? ????????????}?? ?????????????? ????????});?? ????}?? ?????? ????? ? ?? ????OnMultiChoiceClickListener?multiClick?=?new?OnMultiChoiceClickListener(){?? ????????@Override?? ????????public?void?onClick(DialogInterface?dialog,?int?which,?boolean?isChecked)?{?? ????????????Toast.makeText(Lesson_02_Dia.this,?"第"+(which+1)+"項,選中結(jié)果:"+isChecked,Toast.LENGTH_SHORT).show();?? ????????}?? ?????????? ????};?? ?????? ????? ? ?? ????OnClickListener?onclick?=?new?OnClickListener()?{?? ????????@Override?? ????????public?void?onClick(DialogInterface?dialog,?int?which)?{?? ????????????switch?(which)?{?? ????????????????case?Dialog.BUTTON_NEGATIVE:?? ????????????????????Toast.makeText(Lesson_02_Dia.this,?"No..",?? ????????????????????????????Toast.LENGTH_LONG).show();?? ????????????????????break;?? ????????????????case?Dialog.BUTTON_NEUTRAL:?? ????????????????????Toast.makeText(Lesson_02_Dia.this,?"I?don't?know.",?? ????????????????????????????Toast.LENGTH_LONG).show();?? ????????????????????break;?? ????????????????case?Dialog.BUTTON_POSITIVE:?? ????????????????????Toast.makeText(Lesson_02_Dia.this,?"Yes!!",?? ????????????????????????????Toast.LENGTH_LONG).show();?? ????????????????????break;?? ????????????}?? ????????}?? ????};?? }?? ?
?
說明已經(jīng)寫在注釋中了。
?
下面再介紹一種比較常用的式樣,如圖:
?
代碼:
[java]?view plaincopyprint?
???@Override?? ???public?void?onCreate(Bundle?savedInstanceState)?{?? ???????super.onCreate(savedInstanceState);?? ???????setContentView(R.layout.main);?? ????????? ???????Button?button?=?(Button)findViewById(R.id.b01);?? ???????button.setText("對話框");?? ???????button.setOnClickListener(new?Button.OnClickListener(){?? ????????@Override?? ????????public?void?onClick(View?v)?{?? ?????????????? ????????????String[]?choices={"新浪微博","校內(nèi)","街旁"};?? ??????????????????? ????????????AlertDialog?dialog?=?new?AlertDialog.Builder(Lesson_02_Dia.this)?? ?????????????????????.setIcon(android.R.drawable.btn_star)?? ?????????????????????.setTitle("分享")?? ?????????????????????.setItems(choices,?onselect).create();?? ????????????dialog.show();?? ????????}?? ???????});?? ???}?? ????? ????? ? ? ?? ???OnClickListener?onselect?=?new?OnClickListener()?{?? ????@Override?? ????public?void?onClick(DialogInterface?dialog,?int?which)?{?? ?????????? ????????switch?(which)?{?? ????????case?0:?? ????????????Toast.makeText(Lesson_02_Dia.this,?"您選擇了新浪微博.",Toast.LENGTH_SHORT).show();?? ????????????break;?? ????????case?1:?? ????????????Toast.makeText(Lesson_02_Dia.this,?"您選擇了校內(nèi)",Toast.LENGTH_SHORT).show();?? ????????????break;?? ????????case?2:?? ????????????Toast.makeText(Lesson_02_Dia.this,?"您選擇了街旁",Toast.LENGTH_SHORT).show();?? ????????????break;?? ????}?? ????}?? ?????? ???};?? ?
?
好了,今天就寫到這,改天寫一下,如果在彈出框中做一個登陸界面。
?
?
繼續(xù)補充...先上圖...
?
頁面login.xml: 示例寫的比較簡單,布局大家可以自己完善、修改。
[xhtml]?view plaincopyprint?
<?xml?version="1.0"?encoding="utf-8"?>?? <TableLayout?? android:id="@+id/widget36"?? android:layout_width="fill_parent"?? android:layout_height="fill_parent"?? android:orientation="vertical"?? xmlns:android="http://schemas.android.com/apk/res/android"?? >?? <TextView?? android:id="@+id/widget37"?? android:layout_width="wrap_content"?? android:layout_height="wrap_content"?? android:text="用戶名:"?? >?? </TextView>?? <EditText?? android:id="@+id/widget38"?? android:layout_width="wrap_content"?? android:layout_height="wrap_content"?? android:text=""?? android:textSize="18sp"?? >?? </EditText>?? <TextView?? android:id="@+id/widget39"?? android:layout_width="wrap_content"?? android:layout_height="wrap_content"?? android:text="密碼:"?? >?? </TextView>?? <EditText?? android:id="@+id/widget40"?? android:layout_width="wrap_content"?? android:layout_height="wrap_content"?? android:text=""?? android:textSize="18sp"?? >?? </EditText>?? </TableLayout>?? ?
?
代碼 : (也比較簡單)
[c-sharp]?view plaincopyprint?
LayoutInflater?factory?=?LayoutInflater.from(Lesson_02_Dia.this);?? ?? View?view?=?factory.inflate(R.layout.login,?null);?? ?? AlertDialog?dialog02?=?new?AlertDialog.Builder(Lesson_02_Dia.this)?? ?????.setIcon(android.R.drawable.btn_star)?? ?????.setTitle("登錄")?? ???????.setView(view)?? ???????.setPositiveButton("Yes",?onclick)?? ???????.setNegativeButton("No",??onclick).create();?? dialog02.show();?? ?
?
有問題歡迎大家交流。
?
總結(jié)
以上是生活随笔為你收集整理的【Android】对话框 AlertDialog的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。