Android 自定义Dialog (弹出发表情层选择)

    资料来源于网上。    不多说,上代码。 

        第一个布局文件,只放一个Button ,  点击Button之后即可弹出

     布局文件:

 
      
  布局二 :showface

     表情的布局文件,这里用GridView

       

  
    
 

  布局三 :

     GridView 中Item的布局 ,

             只是一个ImageView

  

     好了。布局完成,下面写java代码。

      

	class Tip {        // 弹出表情层的类
        public Tip(Context context) {
			dialog = new Dialog(context, R.style.dialog);   // 定义一个Dialog ,Style样式:设置背景透明,无title,无边框
			Window window = dialog.getWindow();  
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.x = -30;
			wl.y = 20;
			window.setAttributes(wl);
			window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
			window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			dialog.setContentView(R.layout.showface);
			dialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
			faceGrid = (GridView) dialog.findViewById(R.id.updater_faceGrid);
			if (faceGrid.getAdapter() == null) {
				faceGrid.setAdapter(new FaceAdapter(getApplicationContext(),
						Face.faceNames));
			}
			faceGrid.setOnItemClickListener(new OnItemClickListener() {    // Gridview选中,选择一个表情

				@Override
				public void onItemClick(AdapterView parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					Toast.makeText(getBaseContext(), Face.faceNames[position],
							Toast.LENGTH_SHORT).show();
					btnButton.setText(TextUtil.formatImage("["+ Face.faceNames[position] + "]",DialogShowActivity.this)); 
                                         //TextUtil.formatImage 匹配表情
                         dialog.dismiss();
				}
			});
		}

		void showdia() {
			dialog.show();
		}

	}

                   

TextUtil匹配表情类 :

         

	public static SpannableString formatImage(CharSequence text, Context context) {
		SpannableString spannableString = new SpannableString(text);
		Pattern pattern = Pattern.compile("\\[[^0-9]{1,4}\\]"); //正则匹配
		Matcher matcher = pattern.matcher(spannableString); 
		while (matcher.find()) {
			String faceName = matcher.group();
			String key = faceName.substring(1, faceName.length() - 1);
			if (Face.getfaces(context).containsKey(key)) {
				spannableString.setSpan(new ImageSpan(context, Face.getfaces(
						context).get(key)), matcher.start(), matcher.end(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
		}
		return spannableString;
	}



        GridView 适配器  :

              

public class FaceAdapter extends BaseAdapter { 
	
	 private Context context ;
	 private String[] faceNames ; 
	 private HashMap faces ;
	  public FaceAdapter(Context context,String[] faceName)
	  {
		    this.context= context;
		    this.faceNames= faceName ;
		    this.faces=Face.getfaces(context);
	  }
	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount()
	{
		// TODO Auto-generated method stub
		return faceNames==null?0:faceNames.length;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub 
		 ImageView ivImageView= null;
		  if (convertView==null)
		{
			 convertView =View.inflate(context, R.layout.faceimage, null);
			  ivImageView =(ImageView) convertView.findViewById(R.id.imageview_iv); 
			  convertView.setTag(ivImageView);
		}
		  else {
			ivImageView =(ImageView)convertView.getTag();
		} 
		   if (faces.containsKey(Face.faceNames[position])) 
		{
			 ivImageView.setImageResource(faces.get(Face.faceNames[position]));
		}
		return convertView;
	}
}
 

   表情类

            

public class Face {

	public static String[] faceNames = new String[] { "织", "围观", "威武", "奥特曼",
			"爱心传递", "围脖", "温暖帽子", "手套", "雪", "雪人", "落叶", "照相机", "浮云", "帅",
			"礼物", "呵呵", "嘻嘻", "哈哈", "爱你", "晕", "泪", "馋嘴", "抓狂", "哼", "可爱", "怒",
			"汗", "害羞", "睡觉", "钱", "偷笑", "酷", "衰", "吃惊", "闭嘴", "鄙视", "挖鼻屎",
			"花心", "鼓掌", "失望", "思考", "生病", "亲亲", "怒骂", "太开心", "懒得理你", "左哼哼",
			"右哼哼", "嘘", "委屈", "吐", "可怜", "打哈气", "握手", "耶", "good", "弱", "不要",
			"ok", "赞", "来", "蛋糕", "心", "伤心", "猪头", "咖啡", "话筒", "干杯", "绿丝带",
			"蜡烛", "钟", "微风", "月亮", "做鬼脸", "给力", "神马", "互粉", "萌", "熊猫", "鸭梨",
			"兔子" };

	private static HashMap faces;

	public static HashMap getfaces(Context context) {
		if (faces != null) {
			return faces;
		}
		faces = new HashMap();
		String faceName = "";
		for (int i = 217; i <= 290; i++) {
			faceName = "face" + i;
			try {
				int id = R.drawable.class.getDeclaredField(faceName).getInt(
						context);
				faces.put(faceNames[i - 217], id);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}

		faces.put("给力", R.drawable.geili_thumb);
		faces.put("神马", R.drawable.horse2_thumb);
		faces.put("互粉", R.drawable.hufen_thumb);
		faces.put("萌", R.drawable.kawayi_thumb);
		faces.put("熊猫", R.drawable.panda_thumb);
		faces.put("鸭梨", R.drawable.pear_thumb);
		faces.put("兔子", R.drawable.rabbit_thumb);

		return faces;
	}

      好了,这样就可以实现。

          如果要设置Gridiew背景透明 , 还需要加一个Style

          

    

      最后添加表情图片。    (效果图)


             第一次写博客。 说的不是很明白。  ..............

         



你可能感兴趣的:(Android 自定义Dialog (弹出发表情层选择))