当前位置: 代码迷 >> JavaScript >> 各种dialog,alertdialog,AlertDialog.Builder(this)的小结,并封住成javaScript调用
  详细解决方案

各种dialog,alertdialog,AlertDialog.Builder(this)的小结,并封住成javaScript调用

热度:772   发布时间:2012-09-28 00:03:35.0
各种dialog,alertdialog,AlertDialog.Builder(this)的总结,并封住成javaScript调用
package com.emn.emf.android.basecommands.loading;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.emn.emf.android.AndroidBaseActivity;
import com.emn.emf.android.CommandObject;
import com.emn.emf.android.R;

public class Loading extends AndroidBaseActivity implements CommandObject {
	private ProgressDialog myDialog2 = null;
	private ProgressDialog myDialogTime = null;
	private long time = 0,time2 = 0,time3 = 0;
	private CustomDialog cd;
	private Dialog cd2;
	public  AlertDialog alertDialog;
	private String showCenterTipTitle = "",showCenterTipTitle2 = "",showCenterTipTitle3 = "",showCenterTipTitle4 = "";
	private String showCenterTipTime = "",showCenterTipTime2 = "",showCenterCallback3 = null,showCenterTipContent3 = "";
	private String showCenterTipContent = "",showCenterTipContent2 = ""	,showCenterTipTimeTitle = "";;
	private String showCenterCallback4 = null,showCenterTipContent4 = "";
	private String showCenterTipTimeTime = "",showCenterTipTiemContent = "",showCenterCallback = "";
	private Handler mHandler = new Handler();

	@Override
	public Boolean checkValidation(String checkCommand, Object parameters) {
		// TODO Auto-generated method stub
		return Boolean.TRUE;
	}
	/*
	 * (non-Javadoc)
	 * @see com.emn.emf.android.CommandObject#handleIt(java.lang.String, java.lang.Object)
	 */
	@Override
	public Object handleIt(String checkCommand, Object parameters) {

		String jsonstartLoad = "";
		if (checkCommand.equals("startLoad")) {
			jsonstartLoad = getStartLoad(parameters);
		} else if (checkCommand.equals("finishLoad")) {
			jsonstartLoad = getFinishLoading();

		} else if (checkCommand.equals("showCenterTipMyTwoButton")) {
			jsonstartLoad = getshowCenterTipMyTwoButton(parameters);
		} else if (checkCommand.equals("showCenterTipMyTime")) {
			jsonstartLoad = getshowCenterTipMyTime(parameters);
		} else if (checkCommand.equals("showCenterTipButtonTwo")) {
			jsonstartLoad = getshowCenterTipButtonTwo(parameters);
		} 
		else if (checkCommand.equals("showCenterTipButtonOne")) {
			jsonstartLoad = getshowCenterTipBttonOne(parameters);
		}else if (checkCommand.equals("showCenterTipTimeSystem")) {
			jsonstartLoad = getshowCenterTipTimeSystem(parameters);
		}
		return jsonstartLoad;

	}
	/*
	 *  启动对话框,带有系统默认旋转图片,可以自定义标题,和内容提示部分。
	 */
	private String getStartLoad(Object parameters) {

		String loadingTitle = "";
		String loadingContent = "";
		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			loadingTitle = jsonObject.getString("title");
			loadingContent = jsonObject.getString("content");
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		CharSequence strDialogTitle = (CharSequence) loadingTitle;
		CharSequence strDialogBody = (CharSequence) loadingContent;

		// 显示Progress对话框
		myDialog2 = ProgressDialog.show(AndroidBaseActivity.self,
				strDialogTitle, strDialogBody, true);

		

		return "{states:success}";
	}

	
    /*
     * 加载Loading效果,你可以自定义指定标题,内容。但没有自动取消。
     * 如果需要取消需要调用JS方法finishLoading()进行取消。
     */
	private String getshowCenterTipTimeSystem(Object parameters) {
		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			showCenterTipTimeTitle = jsonObject.getString("title");
			showCenterTipTimeTime = jsonObject.getString("time");
			showCenterTipTiemContent = jsonObject.getString("content");
			time2 = Long.parseLong(showCenterTipTimeTime);

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		CharSequence strDialogTitle = (CharSequence) showCenterTipTimeTitle;
		CharSequence strDialogBody = (CharSequence) showCenterTipTiemContent;

		// 显示Progress对话框
		myDialogTime = ProgressDialog.show(AndroidBaseActivity.self,
				strDialogTitle, strDialogBody, true);

		new Thread() {
			public void run() {
				try {
					sleep(time2);
				} catch (Exception e) {
					e.printStackTrace();
				} finally { // 卸载所创建的myDialog对象。
					myDialogTime.dismiss();
				}
			}
		}.start(); // 开始运行运行线程

		return "{states:success}";
	}

	
	/*
	 *  取消Loading效果,和getshowCenterTipTime(Object parameters)配对使用。
	 */
	private String getFinishLoading() {
		if(myDialog2 == null) {
			return "{states:success}";
		}
		myDialog2.dismiss();
		return "{states:success}";
	}
	
	
	
	/*
	 * 自定义dialog,按确定,可以指定执行JS方法,可以指定标题,内容。
	 * 暂时不支持指定时间,需要的话可以随时添加;
	 */
	
	
	private String getshowCenterTipMyTwoButton(Object parameters) {

		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			showCenterTipTitle = jsonObject.getString("title");
			showCenterTipTime = jsonObject.getString("time");
			showCenterTipContent = jsonObject.getString("content");
			showCenterCallback = jsonObject.getString("callback");
			time = Long.parseLong(showCenterTipTime);

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		cd = new CustomDialog(AndroidBaseActivity.self);
		cd.show();
		
		return "{states:success}";
	}

	/*
	 *  自定义对话框
	 */
	class CustomDialog extends Dialog {

		public CustomDialog(Context context) {
			super(context);
		}

		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			setContentView(R.layout.custom_dialog);
			setTitle(showCenterTipTitle);

			TextView text = (TextView) findViewById(R.id.text);
			text.setText(showCenterTipContent);
			ImageView image = (ImageView) findViewById(R.id.image);
			image.setImageResource(R.drawable.icon);

			Button buttonYes = (Button) findViewById(R.id.button_yes);
			buttonYes.setHeight(5);
			buttonYes.setOnClickListener(new Button.OnClickListener() {

				public void onClick(View v) {
					dismiss();
					mHandler.post(new Runnable() {
						public void run() {

							getWebView().loadUrl(
									"javascript:" + showCenterCallback);
						}
					});

				}
			});
			Button buttonNo = (Button) findViewById(R.id.button_no);
			buttonNo.setSingleLine(true);
			buttonNo.setOnClickListener(new Button.OnClickListener() {

				public void onClick(View v) {
					dismiss();

				}
			});
		}
	}
	
	/*
	 * 自定义dialog,可以指定执行时间长度,标题,内容部分支持最大48*48像素PNG图片。
	 * 
	 */
	private String getshowCenterTipMyTime(Object parameters) {

		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			showCenterTipTitle2 = jsonObject.getString("title");
			showCenterTipTime2 = jsonObject.getString("time");
			showCenterTipContent2 = jsonObject.getString("content");
			// showCenterCallback = jsonObject.getString("callback");
			time3 = Long.parseLong(showCenterTipTime2);

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		cd2 = new CustomDialog2(AndroidBaseActivity.self);
		cd2.setContentView(R.layout.custom_dialog2);
		cd2.setTitle(showCenterTipTitle2);
		cd2.show();
		new Thread() {
			public void run() {
				try {
					sleep(time3);
				} catch (Exception e) {
					e.printStackTrace();
				} finally { // 卸载所创建的myDialog对象。
					cd2.dismiss();
				}
			}
		}.start(); // 开始运行运行线程
	return "{states:success}";
	}

	class CustomDialog2 extends Dialog {

		public CustomDialog2(Context context) {
			super(context);
		}

		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);

			setContentView(R.layout.custom_dialog2);
			TextView text = (TextView) findViewById(R.id.text);
		    text.setText(showCenterTipContent2);
			ImageView image = (ImageView) findViewById(R.id.image);
			image.setImageResource(R.drawable.icon);
		}

	}

	/*
	 * 系统dialog的封装,可以指定执行时间,标题,内容。
	 * 有确定按钮,和取消按钮,点击确定时执行一个JS方法,取消不执行。
	 * 
	 */
	
	private String getshowCenterTipButtonTwo(Object parameters) {

		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			showCenterTipTitle3 = jsonObject.getString("title");
			//showCenterTipTime3 = jsonObject.getString("time");
			showCenterTipContent3 = jsonObject.getString("content");
			showCenterCallback3 = jsonObject.getString("callback");
			//time4 = Long.parseLong(showCenterTipTime3);

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		new AlertDialog.Builder(AndroidBaseActivity.self)
	    .setTitle(showCenterTipTitle3)//.setView(view)
	    .setMessage(showCenterTipContent3)
	    .setPositiveButton("确定",
	        new DialogInterface.OnClickListener(){
	            public void onClick(
	                DialogInterface dialoginterface, int i){
	            	getWebView().loadUrl(
							"javascript:" + showCenterCallback3);
	                
	                }
	            })
	    .setNegativeButton("取消",
	        new DialogInterface.OnClickListener(){
	            public void onClick(
	                DialogInterface dialoginterface, int i){
	                }
	            })
	    .show();
		
	return "{states:success}";
	}
	
	
	

	/*
	 * 系统dialog的封装,可以指定执行时间,标题,内容。
	 * 只有确定按钮,,点击确定时执行一个JS方法。
	 * 
	 */
	
	private String getshowCenterTipBttonOne(Object parameters) {

		String strJSONParam = parameters.toString();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(strJSONParam);
			showCenterTipTitle4 = jsonObject.getString("title");
			//showCenterTipTime3 = jsonObject.getString("time");
			showCenterTipContent4 = jsonObject.getString("content");
			 showCenterCallback4 = jsonObject.getString("callback");
			//time4 = Long.parseLong(showCenterTipTime3);

		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		new AlertDialog.Builder(AndroidBaseActivity.self)
	    .setTitle(showCenterTipTitle4)//.setView(view)
	    .setMessage(showCenterTipContent4)
	    .setPositiveButton("确定",
	        new DialogInterface.OnClickListener(){
	            public void onClick(
	                DialogInterface dialoginterface, int i){
	            	
	            	getWebView().loadUrl(
							"javascript:" + showCenterCallback4);
	                }
	            })
	    
	    .show();
		
	return "{states:success}";
	}

	

	
	
}

?

2.custom_dialog.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
               android:background="@drawable/bg3"
              android:padding="10dp"
              >
      <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:padding="2px" 
        >
        
	        <ImageView android:id="@+id/image"
	               android:layout_width="wrap_content"
	               android:layout_height="wrap_content"
	               android:layout_marginRight="2dp"
	               />
	    	<TextView android:id="@+id/text"
	              android:layout_width="wrap_content"
	              android:layout_height="wrap_content"
	              android:textColor="#FFF"
	              android:paddingTop="12dp"
	             
	              />
              
      </LinearLayout>        
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="5px" 
	  >
	          <Button
	              android:id="@+id/button_yes"
	              android:layout_width="wrap_content"
	              android:layout_height="wrap_content"
	              android:text="      确定      "
	              />
	          <Button 
	              android:id="@+id/button_no"
	              android:layout_width="wrap_content"
	              android:layout_height="wrap_content"
	              android:text="      取消       "
	              />
    </LinearLayout>
    
</LinearLayout>

?

?

3.custom_dialog2.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              android:background="@drawable/bg3"
              >
              
              
      <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="2"
        
        android:padding="2px" 
        >
        	
	        <ImageView android:id="@+id/image"
	               android:layout_width="wrap_content"
	               android:layout_height="wrap_content"
	               android:layout_marginRight="2dp"
	               />
	    	<TextView android:id="@+id/text"
	              android:layout_width="wrap_content"
	              android:layout_height="wrap_content"
	              android:paddingTop="18px"
	              android:gravity="center_horizontal"
	             
	             
	              />
              
      </LinearLayout>        
    
    
</LinearLayout>

?4.main.js代码

//加载效果,你可以自定义指定标题,内容。但没有自动取消。如果需要取消需要调用JS方法finishLoading()
function getStartLoading(){
	return emf.makeCall('startLoading.startLoad', "{title:'联系人',content:'请您稍等正在加载联系人...'}");
}
//与上面方法一对,负责关闭上面的方法。
function finishLoading(){
	return emf.makeCall('startLoading.finishLoad', "{}");
}
//自定义dialog,可以指定执行JS方法,可以指定标题,内容。暂时不支持指定时间,需要的话可以随时添加
function showCenterTipMyTwoButton(){
	return emf.makeCall('startLoading.showCenterTipMyTwoButton', "{title:'操作成功',content:'操作成功信息部分',time:'5000',callback:'showCenterTipMyTime()'}");
}
//自定义dialog,可以指定执行时间,标题,内容。内容部分支持最大48*48像素PNG图片
function showCenterTipMyTime(){
	return emf.makeCall('startLoading.showCenterTipMyTime', "{title:'操作成功时间',content:'时间操作成功信息部分',time:'5000'}");
}

//系统dialog的封装,可以指定执行时间,标题,内容。有确定,执行一个JS方法,取消不执行。
function showCenterTipButtonTwo(){
	return emf.makeCall('startLoading.showCenterTipButtonTwo', "{title:'操作标题',content:'时间操作成功信息部分呵呵!',callback:'showCenterTipButtonOne()'}");
}
function showCenterTipButtonOne(){
	return emf.makeCall('startLoading.showCenterTipButtonOne', "{title:'操作标题',content:'时间操作成功信息部分呵呵!',callback:'showCenterTipButtonTwo()'}");
}
//系统自带的Dialog,但可以指定执行时间,标题,内容。
function showCenterTipTimeSystem(){
	return emf.makeCall('startLoading.showCenterTipTimeSystem', "{title:'恭喜操作成功',content:'操作成功信息部分',time:'5000'}");
}

?

?

5.index.html页面代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>EMF</title>
<base href="mobile/">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="file:///android_asset/main.js"
charset="utf-8"></script>


</head>
<body>
<label id='testlabel'>123 </label>
<br />
<br />
<input type="button" value="getStartLoading()" onclick="getStartLoading()" />
<input type="button" value="showCenterTipMyTwoButton()" onclick="showCenterTipMyTwoButton()" />
<input type="button" value="showCenterTipMyTime()" onclick="showCenterTipMyTime()" />
<input type="button" value="showCenterTipTimeSystem()" onclick="showCenterTipTimeSystem()" />
<input type="button" value="showCenterTipButtonTwo()" onclick="showCenterTipButtonTwo()" />
<input type="button" value="showCenterTipButtonOne()" onclick="showCenterTipButtonOne()" />
<br />
</body>
<script type="text/javascript">
//function showCenterTipTest(){

//}

//testlabel.innerText += "abc:" + getDownload();
//getStartLoading();
// window.setTimeout('finishLoading()',6000)
//testlabel.innerText = emf.makeCall('telePhone', '{query:query}');
//testlabel.innerText = "abc:" + getContactsId();
//testlabel.innerText += "abc:" + getDownload();
//testlabel.innerText += "abc:" + getStartLoading();

</script>
</html>
?
  相关解决方案