Android 自定义Dialog (弹出发表情层选择)
来源:互联网 发布:前端如何解析json数据 编辑:程序博客网 时间:2024/05/06 18:04
资料来源于网上。 不多说,上代码。
第一个布局文件,只放一个Button , 点击Button之后即可弹出
布局文件:
<Button android:layout_marginTop="100dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="弹 出" android:textColor="#0099ff" android:textSize="30sp" android:id="@+id/btn" />
布局二 :showface
表情的布局文件,这里用GridView
<GridView android:id="@+id/updater_faceGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:numColumns="8" android:stretchMode="columnWidth" android:verticalSpacing="30.0dip" > </GridView>
布局三 :
GridView 中Item的布局 ,
只是一个ImageView
<ImageView android:id="@+id/imageview_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center" />
好了。布局完成,下面写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选中,选择一个表情@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubToast.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<String, Integer> 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 stubreturn faceNames==null?0:faceNames.length;}/* (non-Javadoc) * @see android.widget.Adapter#getItem(int) */public Object getItem(int position){// TODO Auto-generated method stubreturn position;}/* (non-Javadoc) * @see android.widget.Adapter#getItemId(int) */public long getItemId(int position){// TODO Auto-generated method stubreturn 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<String, Integer> faces;public static HashMap<String, Integer> getfaces(Context context) {if (faces != null) {return faces;}faces = new HashMap<String, Integer>();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
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!-- 边框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">false</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 无标题 --> <item name="android:windowBackground">@color/transparent</item> <!-- 背景透明 --> <item name="android:backgroundDimEnabled">false</item> <!-- 模糊 --> </style>
最后添加表情图片。 (效果图)
第一次写博客。 说的不是很明白。 ..............
- Android 自定义Dialog (弹出发表情层选择)
- android Dialog 自定义弹出层
- android自定义弹出层
- android自定义弹出层
- android 自定义弹出框Dialog
- Android自定义底部弹出Dialog
- Android自定义dialog弹出布局
- Android自定义dialog弹出布局
- android自定义dialog弹出框
- Android自定义dialog弹出布局
- Android--自定义弹出框-自定义dialog
- Android 底部弹出Dialog(支付方式选择)
- android 自定义dialog弹出和消失动画
- android自定义dialog弹出框、透明背景
- android 自定义dialog弹出和消失动画
- [Android]后台Service 弹出自定义dialog
- Android Dialog 弹出框 自定义 样式
- android Dialog 自定义及弹出动画
- 使用nohup让程序永远后台运行
- Ubuntu 10.04LTS 安装tomcat7.0.23
- 浅谈程序员的10个层次
- AdapterView.OnItemClickListener
- windows下下载android源代码步骤
- Android 自定义Dialog (弹出发表情层选择)
- CString保存所有节点信息及读取CString中的节点信息
- 【Jquery mobile】事件
- ExtJs Grid 表格内容链接的方法
- 长城拆开了,不过是一堆散乱的砖头
- iphone 信息提示类
- Java Serialization
- 环境为王-论贴吧环境解决方案
- js数组去重