android中dialog封装
来源:互联网 发布:巴恩斯数据 编辑:程序博客网 时间:2024/05/16 09:49
在开发中经常用到对话框和加载中的进度条,做一个总结。
效果图:
在activity中的代码:
public class MainActivity extends Activity implements OnClickListener {
private Button btn_ok;
private Button btn_list;
private Button btn_myDialog;
private Button btn_progress;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_ok = (Button) findViewById(R.id.btn_ok); btn_list = (Button) findViewById(R.id.btn_list); btn_myDialog=(Button) findViewById(R.id.btn_myDialog); btn_progress=(Button) findViewById(R.id.btn_progress); btn_ok.setOnClickListener(this); btn_list.setOnClickListener(this); btn_myDialog.setOnClickListener(this); btn_progress.setOnClickListener(this);}@Overridepublic void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //系统的对话框; case R.id.btn_ok: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("标题"); builder.setMessage("提示的内容"); builder.setIcon(R.drawable.ic_launcher); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }); builder.create().show(); break; //系统列表框; case R.id.btn_list: AlertDialog.Builder builder2 = new AlertDialog.Builder( MainActivity.this); builder2.setTitle("选择"); builder2.setIcon(R.drawable.ic_launcher); String items[] = { "拍照", "图片库" }; builder2.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int which) { // TODO Auto-generated method stub switch (which) { case 0: Toast.makeText(MainActivity.this, "拍照", 0).show(); break; case 1: Toast.makeText(MainActivity.this, "图片库 ", 0).show(); break; } } }); builder2.show(); break; //自定义封装对话框; case R.id.btn_myDialog: final Dialog_ask dialog = new Dialog_ask(MainActivity.this, "对话框的标题", "提示的内容"); dialog.show(); dialog.setCanel("取消", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT) .show(); dialog.dismiss(); } }); dialog.setOk("确定", new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT) .show(); dialog.dismiss(); } }); break; //自定义正在加载中进度条; case R.id.btn_progress: LoadingDialog load_dialog=new LoadingDialog(this); load_dialog.showDialog(); break; }}
}
封装的Dialog_adk代码:
public class Dialog_ask { Context context; android.app.AlertDialog dialog; TextView tv_title; TextView tv_con; LinearLayout buttonLayout; Button btn_ok; Button btn_canel; /** * @param context * @param title 设置对话框的标题 * @param con 设置对话框的内容 */ public Dialog_ask(final Context context, String title, String con) { // TODO Auto-generated constructor stub this.context = context; dialog = new android.app.AlertDialog.Builder(context).create(); View view = LayoutInflater.from(context).inflate(R.layout.dialog_ask, null); dialog.setView(view); //在这可以设置dialog的一些属性; btn_ok = (Button) view.findViewById(R.id.btn_ok); btn_canel = (Button) view.findViewById(R.id.btn_cancel); tv_title = (TextView) view.findViewById(R.id.tv_title); tv_con = (TextView) view.findViewById(R.id.tv_con); tv_title.setText(title); tv_con.setText(con); } public void show() { dialog.show(); } //确定 public void setOk(String s, final View.OnClickListener listener) { btn_ok.setText(s); btn_ok.setOnClickListener(listener); } //取消 public void setCanel(String s, final View.OnClickListener listener) { btn_canel.setText(s); btn_canel.setOnClickListener(listener); // dismiss(); } public void dismiss() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); } }}
封装的Progress加载中的代码:
public class LoadingDialog {
private Context context;
private ImageView imageView;
private Dialog dialog;
public LoadingDialog(Context context) { this.context = context; dialog = new Dialog(context, R.style.dialog_style); View view = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null); imageView= (ImageView) view.findViewById(R.id.img); Animation animation= AnimationUtils.loadAnimation( context, R.anim.load_animation); // 使用ImageView显示动画 imageView.startAnimation(animation); dialog.setCancelable(false); dialog.setContentView(view);}public void showDialog() { dialog.show();}public void dismissDialog() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); }}
}
代码如下
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <TextView android:id="@+id/title" style="@style/TextAppearance.Info2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#efefef" android:padding="10dp" android:text="选择关闭类型" /> <View android:layout_below="@id/title" android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="1dp" android:background="#50000000" /> <Button android:id="@+id/tv_normal" style="@style/TextAppearance.Info2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/view1" android:background="@drawable/bg_quality_click" android:gravity="left|center_vertical" android:paddingLeft="10dp" android:text="普通关闭" /> <View android:layout_below="@id/tv_normal" android:id="@+id/view2" android:layout_width="match_parent" android:layout_height="1dp" android:background="#10000000" /> <Button android:id="@+id/tv_noStandart" style="@style/TextAppearance.Info2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/view2" android:background="@drawable/bg_quality_click" android:gravity="left|center_vertical" android:paddingLeft="10dp" android:text="无标准" /> <View android:layout_below="@id/tv_noStandart" android:id="@+id/view3" android:layout_width="match_parent" android:layout_height="1dp" android:background="#10000000" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/button_quality_close" android:gravity="center" android:text="取消" /></RelativeLayout>
myDialog = new AlertDialog.Builder(context).create(); myDialog.show(); WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = myDialog.getWindow().getAttributes(); lp.width = display.getWidth()-100; //设置宽度 lp.height=display.getHeight()-200; myDialog.getWindow().setAttributes(lp); myDialog.getWindow().setContentView(R.layout.dialog_close); myDialog.getWindow() .findViewById(R.id.tv_normal) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myDialog.dismiss(); } });
代码下载地址:http://download.csdn.net/detail/androidxiaogang/9235401
2 0
- android中dialog封装
- android Dialog 自定义 封装
- Android Dialog高度封装
- Android 自定义dialog(封装好的)
- Android:系统Dialog的简单封装
- Android 自定义通用Dialog的封装
- Android 自定义Dialog实现步骤及封装
- Android 中Dialog 保持
- android 中自定义dialog
- Android中DiaLog大全
- android中自定义dialog
- Android中自定义Dialog
- Android中自定义dialog
- Android中自定义Dialog
- Android中Dialog
- Dialog封装
- Dialog封装
- android开发中-使用Dialog managed dialog
- Hibernate持久化对象的生命周期详解
- 利用Spring的AbstractRoutingDataSource解决多数据源的问题
- nginx php 配置
- leetcode笔记:House Robber
- Styles.Rende @Scripts.Render 错误
- android中dialog封装
- servlet专题
- 音乐播放器分析——目录及总结
- os 模块
- CF#200 div2 C Rational Resistance(math gcd)
- 树状数组详解(处理在线数据,求逆序对)
- 入门篇:高精度乘法
- Java学习心得(2)——数组变量和new
- poj2635