只需要关注单件模式的用法,最常见的是view的单件封装显示,下面举例dialog和popupwindow使用单件模式的用法
一:popupwindow单件模式
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.DebugUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.zs108.util.ConstVar;
import com.zs108.util.Debugs;
import com.zs108.util.Tool;
import java.util.Map;
public class NoticeWindow implements OnClickListener {
private static final String TAG = NoticeWindow.class.getName();
private Activity mContext;
private PopupWindow mWindow; //不能粘贴,modify 20170419
private RelativeLayout rootView;
public static NoticeWindow instance = null;
private WebView wv;
private RelativeLayout notice;
private LinearLayout wait;
private ImageButton close;
private int ext;
private ImageButton upToTop;
private NoticeWindow(){
Debugs.debug("NoticeWindow");
}
public static NoticeWindow getInstance(){
if(instance == null){
instance = new NoticeWindow();
}
return instance;
}
private void initParams(Activity activity){
try {
this.mContext = activity;
} catch (Exception e) {
e.printStackTrace();
}
}
public void showNoticeView(Activity context, int uexit){
if (context == null){
return ;
}
ext = uexit;
initParams(context);
showWindowView();
}
private void showWindowView(){
try {
if(mWindow == null){
Debugs.debug("初始化窗口");
initPopupWindow();
}
mWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void release(){
if(mContext != null){
mContext = null;
}
if(mWindow != null){
mWindow = null;
}
if(null != instance){
instance = null;
}
}
private void initPopupWindow(){
try {
rootView = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.notice, null);
rootView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
initControl();
getNotice();
mWindow = new PopupWindow(rootView);
mWindow.setFocusable(true);
mWindow.setBackgroundDrawable(new BitmapDrawable());
mWindow.setOutsideTouchable(false);
mWindow.setWindowLayoutMode(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initControl(){
try{
notice = (RelativeLayout)rootView.findViewById(R.id.layout_notice);
notice.setVisibility(View.GONE);
wv = (WebView)rootView.findViewById(R.id.webNotice);
wv.setBackgroundColor(0);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wait = (LinearLayout)rootView.findViewById(R.id.layout_wait);
close = (ImageButton)rootView.findViewById(R.id.img_close);
close.setOnClickListener(this);
upToTop = (ImageButton)rootView.findViewById(R.id.upToTop);
upToTop.setOnClickListener(this);
}catch(Exception e){
Debugs.debug("Err in LoaingActiviey initControl : " + e.toString());
}
}
private void getNotice(){
wv.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view,String url){
if(view.getId() == wv.getId()){
closeDialog();
}
}
@Override
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl){
Toast.makeText(mContext, "加载错误 " + description, Toast.LENGTH_LONG).show();
}
});
wv.loadUrl(Const.URL);
}
private void closeDialog(){
wait.setVisibility(View.GONE);
notice.setVisibility(View.VISIBLE);
close.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View arg0){
int id = arg0.getId();
if(id == close.getId()){
hideWindow();
release();
}else if(id == upToTop.getId()){
wv.scrollTo(0, 0);
}
}
public void hideWindow(){
if (mWindow != null) {
mWindow.dismiss();
mWindow = null;
}
}
}
二: Dialog单件模式
package com.zs108.GameDotaLoL.view;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.DebugUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.huang.yzzjmj.R;
import com.zs108.GameDotaLoL.activity.NoticeActivity;
import com.zs108.GameDotaLoL.area.GameSDK;
import com.zs108.GameDotaLoL.area.GameSDK_do;
import com.zs108.GameDotaLoL.data.InfoControl;
import com.zs108.Interface.GameAPIConst;
import com.zs108.Interface.GameAPIFromCPP;
import com.zs108.report.ReportInfo;
import com.zs108.task.LoginTaskAction;
import com.zs108.util.ConstVar;
import com.zs108.util.Debugs;
import com.zs108.util.Tool;
import java.util.Map;
public class NoticeWindow implements OnClickListener {
private static final String TAG = NoticeWindow.class.getName();
private Activity mContext;
private AlertDialog dlg;
private Window dlgwindow;
// private PopupWindow mWindow; //不能粘贴,modify 20170419
// private RelativeLayout rootView;
public static NoticeWindow instance = null;
private WebView wv;
private RelativeLayout notice;
private LinearLayout wait;
private ImageButton close;
private int ext;
private ImageButton upToTop;
private NoticeWindow(){
Debugs.debug("NoticeWindow");
}
public static NoticeWindow getInstance(){
if(instance == null){
instance = new NoticeWindow();
}
return instance;
}
private void initParams(Activity activity){
try {
this.mContext = activity;
} catch (Exception e) {
e.printStackTrace();
}
}
public void showNoticeView(Activity context, int uexit){
if (context == null){
return ;
}
ext = uexit;
initParams(context);
showWindowView();
}
private void showWindowView(){
try {
if(dlg == null){
Debugs.debug("初始化窗口");
initPopupWindow();
}else{
dlg.show();
Debugs.debug("dialog 不为空,显示");
}
// if(mWindow == null){
// Debugs.debug("初始化窗口");
// initPopupWindow();
// }
// mWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
public void release(){
if(mContext != null){
mContext = null;
}
if(dlgwindow != null){
dlgwindow = null;
}
if(null != instance){
instance = null;
}
}
private void initPopupWindow(){
try {
dlg = new AlertDialog.Builder(mContext).create();
dlg.setCanceledOnTouchOutside(false);
dlg.show();
dlgwindow = dlg.getWindow();
dlgwindow.setContentView(R.layout.notice);
WindowManager.LayoutParams params = dlgwindow.getAttributes();
params.width = LayoutParams.MATCH_PARENT;
params.height = LayoutParams.WRAP_CONTENT;
params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
// params.dimAmount=0.7f;//设置对话框的透明程度背景(非布局的透明度)
params.dimAmount=0.1f;//设置对话框的透明程度背景(非布局的透明度)
dlgwindow.setAttributes(params);
dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK){
Debugs.debug( "屏蔽返回键");
return true;
}else{
return false;
}
}
});
initControl();
getNotice();
// rootView = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.notice, null);
// rootView.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// }
// });
//
// initControl();
// getNotice();
//
// mWindow = new PopupWindow(rootView);
// mWindow.setFocusable(true);
// mWindow.setBackgroundDrawable(new BitmapDrawable());
// mWindow.setOutsideTouchable(false);
// mWindow.setWindowLayoutMode(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initControl(){
try{
notice = (RelativeLayout)dlgwindow.findViewById(R.id.layout_notice);
notice.setVisibility(View.GONE);
wv = (WebView)dlgwindow.findViewById(R.id.webNotice);
wv.setBackgroundColor(0);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wait = (LinearLayout)dlgwindow.findViewById(R.id.layout_wait);
close = (ImageButton)dlgwindow.findViewById(R.id.img_close);
close.setOnClickListener(this);
// close.setVisibility(View.INVISIBLE);
upToTop = (ImageButton)dlgwindow.findViewById(R.id.upToTop);
upToTop.setOnClickListener(this);
}catch(Exception e){
Debugs.debug("Err in LoaingActiviey initControl : " + e.toString());
}
}
private void getNotice(){
wv.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view,String url){
if(view.getId() == wv.getId()){
closeDialog();
}
}
@Override
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl){
Toast.makeText(mContext, "加载错误 " + description, Toast.LENGTH_LONG).show();
}
});
wv.loadUrl(GameAPIConst.GAME_NOTICE_URL);
}
private void closeDialog(){
wait.setVisibility(View.GONE);
notice.setVisibility(View.VISIBLE);
close.setVisibility(View.VISIBLE);
}
@Override
public void onClick(View arg0){
int id = arg0.getId();
if(id == close.getId()){
hideWindow();
release();
}else if(id == upToTop.getId()){
wv.scrollTo(0, 0);
}
}
public void hideWindow(){
if (dlg != null) {
dlg.dismiss();
dlg = null;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/notice_bg"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layout_notice"
android:layout_marginLeft="6dp"
android:layout_marginRight="5dp"
android:layout_marginTop="100dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/webNotice"
android:layout_width="250dp"
android:layout_height="290dp"
android:scrollbarStyle="insideOverlay"
android:layout_gravity="center"
android:scrollbars="none"
/>
<ImageButton
android:id="@+id/upToTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/webNotice"
android:layout_alignRight="@+id/webNotice"
android:layout_marginTop="-42dp"
android:background="@drawable/up_to_top" />
</RelativeLayout>
<LinearLayout
android:id="@+id/layout_wait"
android:layout_width="fill_parent"
android:layout_height="290dp"
android:layout_marginTop="100dp"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/dialog"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="@string/getnotice" />
</LinearLayout>
<ImageButton
android:id="@+id/img_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="@drawable/close"
android:background="@drawable/notice_close_button"/>
</LinearLayout>
</RelativeLayout>