Android入门/AlertDialog(十)
来源:互联网 发布:西门子plc编程软件xp 编辑:程序博客网 时间:2024/05/17 23:22
1. 实现系统自带的AlertDialog
2. 实现 自定义Layout的AlertDialog
3. 更改AlertDialog的Theme
先上效果图
1. main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/yes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用Layout的对话框"/><Button android:id="@+id/no" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="默认样式的对话框"/></LinearLayout>2. 自定义AlertDialog的布局文件dialog.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="vertical"><EditText android:layout_height="wrap_content"android:layout_width="fill_parent" android:layout_marginLeft="20dip"android:layout_marginRight="20dip" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/edtInput"/></LinearLayout>
android:textAppearance= "";
3. java代码public class MainActivity extends Activity {private Button yes;private Button no; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); preInitUI(); postInitUI(); } public void preInitUI() { setContentView(R.layout.activity_main); yes = (Button)findViewById(R.id.yes); no = (Button)findViewById(R.id.no); } public void postInitUI() { yes.setOnClickListener(listener); no.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() {public void onClick(View v) {switch (v.getId()) {case R.id.no:showDialog(MainActivity.this);break;case R.id.yes:showDialog_Layout(MainActivity.this);break;}}};// 显示基本的Dialogpublic void showDialog(Context context){// 1. 创建一个builder对象AlertDialog.Builder builder = new AlertDialog.Builder(context);// 2.set 相关的值 或属性builder.setIcon(R.drawable.xuehua); builder.setTitle("Title"); builder.setMessage("Message"); builder.setPositiveButton("Name A", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) { setTitle("点击了对话框上的Name A"); } }); builder.setNeutralButton("Name B", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) { setTitle("点击了对话框上的Name B"); } }); builder.setNegativeButton("Name C", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) { setTitle("点击了对话框上的Name C"); } }); // 展示 builder.show();} public void showDialog_Layout(Context context){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // 绑定 布局 View view = inflater.inflate(R.layout.dialog, null); //到绑定布局内的 子控件 final EditText edtInput=(EditText)view.findViewById(R.id.edtInput); AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AppTheme)); builder.setCancelable(false);//设置为false,按返回键不能退出。默认为true。 builder.setIcon(R.drawable.hong); builder.setTitle("Title"); builder.setView(view); builder.setPositiveButton("确认",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle(edtInput.getText()); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { finish(); } }); builder.show(); } }
- Android入门/AlertDialog(十)
- Android入门/AlertDialog(十)
- Android入门之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门之AlertDialog(纯java)
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog .
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android入门第九篇之AlertDialog
- Android开发入门之AlertDialog使用总结
- Android入门第九篇之AlertDialog
- AlertDialog入门
- android alertdialog
- UVa 10718 - Bit Mask
- 已知两点经纬度,求这两点连成的直线的角度
- POJ 1733 Parity game
- 数据结构与算法分析-第一章Java类(02)
- 如何判断一个指定的经纬度点是否落在一个多边形内
- Android入门/AlertDialog(十)
- 静态数据成员
- 身份证号码检测
- arm-linux-gcc安装
- Redhat linux ftp问题
- 组合模式(Composite)
- Hibernate 一对一主键单向、双向关联
- dynamic_cast
- RyanCoder代码生成器 For SqlServer版