zoukankan      html  css  js  c++  java
  • 给大家共享一个可以在任何界面调用的Dialog

    UiHelper.java

    package com.show.act;
    
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    public class UiHelper {
    	
    	/**
    	 * 提问框的 Listener
    	 * @author Lei
    	 */
    	//因为本类不是activity所以通过继承接口的方法获取到点击的事件
    	public interface OnClickYesListener {
    		abstract void onClickYes();
    	}
    	
    	/**
    	 * 提问框的 Listener
    	 * @author Lei
    	 */
    	public interface OnClickNoListener {
    		abstract void onClickNo();
    	}
    public static void showQuestionDialog(Context context, String title, String text, final OnClickYesListener listenerYes, final OnClickNoListener listenerNo) {
        	
    		Builder builder = new AlertDialog.Builder(context);
    
    		if (!isBlank(text)) {
    			//此方法为dialog写布局
    	    	final TextView textView = new TextView(context);
    	    	textView.setText(text);
    	    	LinearLayout layout = new LinearLayout(context);
    	    	layout.setPadding(10, 0, 10, 0);
    	    	layout.addView(textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    			builder.setView(layout);
    		}
        	//设置title
    		builder.setTitle(title);
    		//设置确定按钮,固定用法声明一个按钮用这个setPositiveButton
    		builder.setPositiveButton(context.getString(R.string.yes), new OnClickListener() {
    	    	public void onClick(DialogInterface dialog, int which) {
    	    		//如果确定被电击
    	    		if (listenerYes != null) {
    	    			listenerYes.onClickYes();
    	    		}
    	    	}
    	    });
    		//设置取消按钮,固定用法声明第二个按钮要用setNegativeButton
    		builder.setNegativeButton(context.getString(R.string.no), new OnClickListener() {
    			public void onClick(DialogInterface dialog, int which) {
    				//如果取消被点击
    				if (listenerNo != null) {
    					listenerNo.onClickNo();
    				}
    			}
    		});
    		
    		//控制这个dialog可不可以按返回键,true为可以,false为不可以
    		builder.setCancelable(true);
    		//显示dialog
    		builder.create().show();
    
    	}
    /**
     * 处理字符的方法
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
    	int strLen;
    	if (str == null || (strLen = str.length()) == 0) {
    		return true;
    	}
    	for (int i = 0; i < strLen; i++) {
    		if ((Character.isWhitespace(str.charAt(i)) == false)) {
    			return false;
    		}
    	}
    	return true;
    }
    }
    

      

    ShowDialogActivity.java

    package com.show.act;
    
    import com.show.act.UiHelper.OnClickNoListener;
    import com.show.act.UiHelper.OnClickYesListener;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class ShowDialogActivity extends Activity {
    //声明Button
      private Button showDialogButton;
      
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init();
        }
        
        private void init(){
        	showDialogButton = (Button)findViewById(R.id.showDialog01);
        	 showDialogButton.setOnClickListener(new OnClickListener() {
    			
    			public void onClick(View arg0) {
    				
    				//调用工具类中的dialog
    				//需要传三个值到showQuestionDialog("当前界面","标题","提示内容",new 确定,new 取消 );
    				
    				UiHelper.showQuestionDialog(ShowDialogActivity.this, "提示", "是否确定", new OnClickYesListener() {
    					
    					public void onClickYes() {
    						// TODO Auto-generated method stub
    						//点击确定干什么
    						
    					}
    				}, new OnClickNoListener() {
    					
    					public void onClickNo() {
    						// TODO Auto-generated method stub
    						//点击取消干什么
    					}
    				});
    				
    			}
    		});
        }
    }
    

      

  • 相关阅读:
    linux常用命令
    mysql 开发基础系列20 事务控制和锁定语句(上)
    sql server 性能调优之 资源等待 CXPACKET
    mysql 开发基础系列19 触发器
    mysql 开发基础系列18 存储过程和函数(下)
    mysql 开发基础系列17 存储过程和函数(上)
    sql server 性能调优之 资源等待PAGEIOLATCH
    mysql 开发基础系列16 视图
    mysql 开发基础系列15 索引的设计和使用
    sql server 性能调优之 当前用户请求分析 (1)
  • 原文地址:https://www.cnblogs.com/firecode/p/2760673.html
Copyright © 2011-2022 走看看