很简单的一个的东西,写了两天,才搞出来,真心汗。想想整整7个月没看一眼代码。突然回来工作,很不适应,脑子里也不知道该怎么下手了。
package com.closedialog; import java.util.ArrayList; import java.util.List; import android.app.Dialog; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.testdlg.R; public class DialogFactory { private Context context; private Dialog dialog; private boolean isFold = false; public DialogFactory(Context context) { this.context = context; } /** * 查询所有支持分享的引用 * * @param context * @return */ private List<ResolveInfo> getShareApps(Context context) { List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); Intent intent = new Intent(Intent.ACTION_SEND, null); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("text/plain"); PackageManager pManager = context.getPackageManager(); mApps = pManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); return mApps; } /** * 得到应用列表 * * @return */ private List<AppInfo> getShareAppList() { List<AppInfo> shareAppInfos = new ArrayList<AppInfo>(); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resolveInfos = getShareApps(context); if (null == resolveInfos) { return null; } else { for (ResolveInfo resolveInfo : resolveInfos) { AppInfo appInfo = new AppInfo(); appInfo.setAppPkgName(resolveInfo.activityInfo.packageName); appInfo.setAppLauncherClassName(resolveInfo.activityInfo.name); appInfo.setAppName(resolveInfo.loadLabel(packageManager).toString()); appInfo.setAppIcon(resolveInfo.loadIcon(packageManager)); shareAppInfos.add(appInfo); Log.v("TAG", "appInfo.getAppName()" + appInfo.getAppName()); } } return shareAppInfos; } /** * 分享对话框 * @param downUrl * 下载地址 * */ public void showDialogShare(final String downUrl) { dialog = new Dialog(context, R.style.dialog); dialog.setContentView(R.layout.dialog_factory_share); ListView listView = (ListView) dialog.findViewById(R.id.list_share); final ShareCustomAdapter adapter = new ShareCustomAdapter(getShareAppList(), context); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent shareIntent = new Intent(Intent.ACTION_SEND); AppInfo app = getShareAppList().get(position); shareIntent.setComponent(new ComponentName(app.getAppPkgName(), app.getAppLauncherClassName())); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, downUrl); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(shareIntent); dialog.dismiss(); } }); Button btn_close = (Button) dialog.findViewById(R.id.btn_dialog_close); btn_close.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } /** * * @param priceBytime * 按次购买价格 * @param priceByMonth * 包月价格 * @param timeRemain * 按次购买提醒内容 * @param monthRemain * 包月购买提醒内容 * @param orderRemain * 订购提示 * @param onClickListener * 点击事件 */ public void showDialogOrder(String priceBytime, String priceByMonth, String timeRemain, String monthRemain, String orderRemain, final BtnClickListener onClickListener) { dialog = new Dialog(context, R.style.dialog); dialog.setContentView(R.layout.dialog_factory_order); TextView tv_ordorByTime = (TextView) dialog.findViewById(R.id.tv_orderByTime); if (priceBytime != null) { tv_ordorByTime.setText(priceBytime); } tv_ordorByTime.setText("按次:" + priceBytime + "元"); // 按次订购按钮 Button btn_orderByTime = (Button) dialog.findViewById(R.id.btn_orderByTime); btn_orderByTime.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (onClickListener != null) { onClickListener.btnOrderByTime(v); } } }); TextView tv_orderByMonth = (TextView) dialog.findViewById(R.id.tv_orderByMonth); if (priceByMonth != null) { tv_orderByMonth.setText("包月:" + priceByMonth + "元(天翼视讯全能看)"); } // 包月订购按钮 Button btn_ordrByMonth = (Button) dialog.findViewById(R.id.btn_orderByMonth); btn_ordrByMonth.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (onClickListener != null) { onClickListener.btnOrderByMonth(v); } } }); TextView tv_remain_time = (TextView) dialog.findViewById(R.id.tv_remain_time); if (timeRemain != null || "".equals(timeRemain)) { tv_remain_time.setText(timeRemain); } else { LinearLayout ll = (LinearLayout) dialog.findViewById(R.id.ll_orderByTime); View view = dialog.findViewById(R.id.view_line_3); ll.setVisibility(View.GONE); view.setVisibility(View.GONE); } TextView tv_remain_month = (TextView) dialog.findViewById(R.id.tv_remain_month); if (monthRemain != null || "".equals(monthRemain)) { tv_remain_month.setText(monthRemain); } else { LinearLayout ll = (LinearLayout) dialog.findViewById(R.id.ll_orderByMonth); View view = dialog.findViewById(R.id.view_line_4); ll.setVisibility(View.GONE); view.setVisibility(View.GONE); } final TextView tv_remain_content = (TextView) dialog.findViewById(R.id.tv_remain_content); final Button btn_fold = (Button) dialog.findViewById(R.id.btn_fold); btn_fold.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (!isFold) { isFold = true; tv_remain_content.setVisibility(View.VISIBLE); btn_fold.setBackgroundResource(R.drawable.btn02_open); } else { isFold = false; tv_remain_content.setVisibility(View.GONE); btn_fold.setBackgroundResource(R.drawable.btn02_close); } } }); TextView tv_remain_conten_TextView = (TextView) dialog.findViewById(R.id.tv_remain_content); tv_remain_conten_TextView.setMovementMethod(new ScrollingMovementMethod()); if (orderRemain != null) { tv_remain_conten_TextView.setText(orderRemain); } // 关闭按钮 Button btn_close = (Button) dialog.findViewById(R.id.btn_close); btn_close.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (onClickListener != null) onClickListener.btnCloseClickListener(v); dialog.dismiss(); } }); dialog.show(); } /** * 一个按钮 * * @param title * 标题 * @param content * 提示内容 * @param okContent * dialog左边按钮内容 * @param onClickListener * @param isCancelable */ public void showDialog(String title, String content, String okContent, final BtnClickListener onClickListener, boolean isCancelable) { showDialog3Btn(title, content, okContent, null, null, onClickListener, isCancelable); } /** * 2个按钮 * * @param title * 标题 * @param content * 提示内容 * @param okContent * dialog左边按钮内容 * @param cancleContent * dialog右边按钮内容 * @param onClickListener * 点击事件 * @param isCancelable * 能否被返回键取消 */ public void showDialog2Btn(String title, String content, String okContent, String cancleContent, final BtnClickListener onClickListener, boolean isCancelable) { showDialog3Btn(title, content, okContent, null, cancleContent, onClickListener, isCancelable); } /** * 3个按钮 * * @param title * 提示标题 * @param content * 提示内容 * @param okContent * dialog左边确定按钮文字 * @param selDefingContent * dialog中间自定义按钮文字 * @param cancleContent * dialog右边取消按钮文字 * @param onClickListener * 点击事件 * @param isCancelable * 能否响应返回键 */ public void showDialog3Btn(String title, String content, String okContent, String selDefingContent, String cancleContent, final BtnClickListener onClickListener, boolean isCancelable) { dialog = new Dialog(context, R.style.dialog); dialog.setCancelable(isCancelable); dialog.setContentView(R.layout.dialog_factory_1_button); Button btnOpt = (Button) dialog.findViewById(R.id.dialog_factory_bt_opt); Button btnCancel = (Button) dialog.findViewById(R.id.dialog_factory_bt_close); Button btnSeldefine = (Button) dialog.findViewById(R.id.dialog_factory_bt_ok); btnSeldefine.setVisibility(View.VISIBLE); Button btn_bottom_cancle = (Button) dialog.findViewById(R.id.dialog_factory_bt_cancle); btn_bottom_cancle.setVisibility(View.VISIBLE); TextView tvContent = (TextView) dialog.findViewById(R.id.dialog_factory_tv_content); tvContent.setMovementMethod(new ScrollingMovementMethod()); TextView tvTitle = (TextView) dialog.findViewById(R.id.dialog_factory_tv_title); tvTitle.setText((null == title) ? "" : title); if (content.length() > 19) { tvContent.setTextSize(22); } if (tvContent != null) { tvContent.setText((null == content) ? "" : content); } if (okContent != null) { btnOpt.setText(okContent); } if (selDefingContent != null) { btnSeldefine.setText(selDefingContent); } else { btnSeldefine.setVisibility(View.GONE); } if (cancleContent != null) { btn_bottom_cancle.setText(cancleContent); } else { btn_bottom_cancle.setVisibility(View.GONE); } btnOpt.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (onClickListener != null) onClickListener.btnOKOnClickListener(v); dialog.dismiss(); } }); if (selDefingContent != null) { btnSeldefine.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (onClickListener != null) onClickListener.btnOKOnClickListener(v); dialog.dismiss(); } }); } if (cancleContent != null) { btn_bottom_cancle.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (onClickListener != null) onClickListener.btnOKOnClickListener(v); dialog.dismiss(); } }); } btnCancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (onClickListener != null) onClickListener.btnCloseClickListener(v); dialog.dismiss(); } }); dialog.show(); } public interface BtnClickListener { /** * 确认按钮 * * @param v */ public abstract void btnOKOnClickListener(View v); /** * dialog中间自定义按钮 * * @param v */ public abstract void btnSelfdefineClickListener(View v); /** * dialog最右边按钮 * * @param v */ public abstract void btnCancleClickListener(View v); /** * 右上角关闭 * * @param v */ public abstract void btnCloseClickListener(View v); /** * 按次购买 * * @param v */ public abstract void btnOrderByTime(View v); /** * 包月购买 * * @param v */ public abstract void btnOrderByMonth(View v); } }