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>
- Android_Dialog对话框_AlertDialog
- android_dialog
- Android_Dialog
- Android_Dialog介绍
- Android_Dialog介绍
- 427_AlertDialog和Builder
- 262_AlertDialog简单模式使用
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 对话框
- 1055. The World's Richest (25)-PAT
- 拿到Android 手機 Sqlite 設置
- 从 Qt 的 delete 说开来
- 进程调度与管理
- PHP中echo,print(),print_r()的区别
- Android_Dialog对话框_AlertDialog
- Citrix 服务器虚拟化之十五 桌面虚拟化之部署XenDesktop7.0
- hdu 2899 (数学+二分)
- xorg 架构 将来 以及一些基本常识浅析
- java常用类————Date类
- hdu 4611 Balls Rearrangement (13多校#2-A)
- 高通平台android开发总结
- ubuntu修改用户名称和计算机名称
- 15 steps to launch your own startup in Europe