Android_Dialog对话框_AlertDialog

来源:互联网 发布:苏尚卿 知乎 编辑:程序博客网 时间:2024/06/05 20:27

1.AlertDialog要素 

(1).Title

可选项,在必要的时候可以设定。

(2).Content area

消息,列表或者布局

(3).Action buttons

可以设定不超过3个按钮,Positive(确定按钮)Negative(取消按钮)Neutral(忽略按钮)

2.AlertDialog实例

button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {//1.创建AlertDialog.Builder预对象AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);/* * 2.设置builder相关属性 * Title:图标icon,标题title * Content area:消息message,列表list或者布局layout * Action buttons:Positive,Negative,Neutral */builder.setIcon(R.drawable.ic_launcher).setTitle("提示");builder.setPositiveButton("确定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("返回", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();}});//3.得到AlertDialog对象(根据builder预对象,创建AlertDialog对象)AlertDialog dialog = builder.create();//4.AlertDialog对象显隐操作dialog.show();//显示dialogdialog.dismiss();//隐藏dialog}});

3.添加Button按钮

The set...Button() methods require a titlefor the button and aDialogInterface.OnClickListenerthat defines the action to take when the user presses the button.最多包含以下其中一个

(1).Positive

You should use this to accept and continue with the action (the "OK" action).

builder.setPositiveButton("确定", new OnClickListener(){});

(2).Negative

You should use this to cancel the action.

builder.setNegativeButton("取消", new OnClickListener(){});

(3).Neutral

builder.setNeutralButton("忽略", new OnClickListener(){});

4.添加列表

 

builder.setMultiChoiceItems(items, checkedItems, listener);builder.setSingleChoiceItems(items, checkedItem, listener);builder.setItems(items, listener);
builder.setMultiChoiceItems(new CharSequence[]{"北京","天津","上海","武汉"}, null, new OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {System.out.println(which);}});

5.自定义布局

public class MainActivity extends Activity {private Button button;private EditText text;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) this.findViewById(R.id.button1);button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setIcon(R.drawable.ic_launcher).setTitle("提示");//view 加载xml布局文件View view = getLayoutInflater().inflate(R.layout.content, null);//将该view加入builder中builder.setView(view);builder.setPositiveButton("确定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();}});builder.setNegativeButton("返回", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();}});//创建AlertDialogAlertDialog dialog = builder.create();dialog.show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
<?xml version="1.0" encoding="utf-8"?><!-- content.xml --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <EditText        android:id="@+id/username"        android:inputType="textEmailAddress"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="16dp"        android:hint="username" />    <EditText        android:id="@+id/password"        android:inputType="textPassword"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="16dp"        android:hint="password"/></LinearLayout>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信红包没领取就删除好友了怎么办 老公一个人出去玩不愿带老婆怎么办 干了一年也攒不下钱怎么办 住院出院后请领导同事吃饭怎么办 有同事请吃饭答应了又不想去怎么办 同事请吃饭发红包不收怎么办 同事要走了请吃饭我不想去怎么办 请领导吃饭他说今晚有事我该怎么办 两个月的狗狗不吃狗粮怎么办 挖机驾驶室的小镙杆总是掉该怎么办 开挖机出了事故没有证怎么办 发现小孩有早恋现象做家长该怎么办 马上中考了初三孩子不上学怎么办 初三孩子老说自己难受不上学怎么办 幼儿园安全教案迷路了怎么办及反思 老婆发现我出轨后离家出走了怎么办 把丝袜放进马桶水箱堵了怎么办 如果老公那方面不行但人很好怎么办 老公哪里都好就是那方面不行怎么办 电饭锅盖子卡的地方掉了怎么办 唯品会退货已经收到了不退款怎么办 有人用qq冒充我和别人聊天怎么办 别人冒充我的微信名和头像怎么办 快餐店无证经营被工商局查了怎么办 买的圣才电子书只有在线版怎么办 吃了汤汤水水奶水还是少怎么办 粗肋草叶子和劲长白点怎么办 海鲜泡的时间长了有味了怎么办 富贵竹新长出来的叶子发黄怎么办 家里养的竹子的叶子发黄怎么办 发财树叶子发黄发黑怎么办打蔫 装修公司把衣柜型号弄错了怎么办 隔水炖锅里的陶瓷坏了怎么办? 色选大米带出比大怎么办? 做手足印如果漆刷到外面了怎么办 小区房子整租给民工影响邻居怎么办 种植牙植体中间的螺丝滑丝怎么办 宝宝1岁了牙齿表面脱落了怎么办 1岁3个月宝宝牙齿发黑脱落怎么办 种植牙中央螺栓断在牙根里怎么办 欧联杯冠军已经获得欧冠资格怎么办