使用edittext直接添加qq表情

建立布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<EditText android:id="@+id/edittext" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:layout_marginTop="10dp"></EditText>
	<Button android:id="@+id/button" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="添加QQ表情"></Button>
</LinearLayout>



编写代码(获取空间 ,注册按钮的点击事件。注意使用bitmap来添加对象)

package com.example.android_textview;

import java.lang.reflect.Field;
import java.util.Random;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	private EditText editText;
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) this.findViewById(R.id.edittext);
		button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int randomId = 1 + new Random().nextInt(9);
				try {
					Field field = R.drawable.class.getDeclaredField("face"
							+ randomId);
					int resourceId = Integer.parseInt(field.get(null)
							.toString());
					// 在android中要显示图片信息,必须使用Bitmap位图的对象来装载
					Bitmap bitmap = BitmapFactory.decodeResource(
							getResources(), resourceId);
					ImageSpan imageSpan = new ImageSpan(MainActivity.this, bitmap);
					SpannableString spannableString = new SpannableString(
							"face");
					spannableString.setSpan(imageSpan, 0, 4,
							Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
					editText.append(spannableString);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}



总结: 使用图像就是使用bitmap这个类。

获得图片资源总是使用field这个类进行反射后进行资源加载。


拓展:就是直接使用一个变化的字符串将图像进行随机取出来了。然后在接着利用反射,将文件的名字转化为实际的对象就是了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值