首页 > 代码库 > 在EditText插入表情,并发送表情
在EditText插入表情,并发送表情
在EditText插入表情,点击发送按钮,将qq表情显示在TextView中;
[mw_shl_code=java,true]public class EditTextActivity extends Activity implements OnClickListener {
private TextView showText, insetBtn;
private EditText edit;
private Button okBtn, backBtn;
private myEditText medit;
private imageAdapter mAdapter;
private GridView mGridview;
// Editable 是一个接口,巧妙的在接口中实现了工厂方法。
private Editable.Factory mEditableFactory = Editable.Factory.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editimage);
mAdapter = new imageAdapter(this);
findView();
}
private void findView() {
// TODO Auto-generated method stub
showText = (TextView) findViewById(R.id.show_text);
insetBtn = (TextView) findViewById(R.id.inset_Btn);// 插入表情
insetBtn.setOnClickListener(this);
edit = (EditText) findViewById(R.id.edit);
okBtn = (Button) findViewById(R.id.ok_btn);// 发送表情
okBtn.setOnClickListener(this);
backBtn = (Button) findViewById(R.id.back_btn);
backBtn.setOnClickListener(this);
medit = (myEditText) findViewById(R.id.edit01);
medit.setText("sssss");
medit.insertIcon(R.drawable.smile);
medit.setVisibility(View.GONE);
mGridview = (GridView) findViewById(R.id.start_grid);
mGridview.setAdapter(mAdapter);
mGridview.setOnItemClickListener(listener);
mGridview.setVisibility(View.GONE);
}
/** 选择表情 **/
OnItemClickListener listener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int clickedImageId = imageAdapter.express[arg2];
mGridview.setVisibility(View.GONE);
CharSequence cs = Html.fromHtml("", imageGetter, null);
edit.append(cs);
// inputLable.append(Html.fromHtml("",
// imageGetter, null));
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.inset_Btn:// 插入表情
mGridview.setVisibility(View.VISIBLE);
break;
case R.id.ok_btn:// 发送表情
Log.v("edit==", edit.getText().toString());
// 将edit中的内容转换为Html
Editable t = mEditableFactory.newEditable(edit.getText());
// System.out.println("edit.getText():"+t+";"+t.toString());
// 封装文本信息及图片,转换为Html格式,并去除<p></p>
String chatStr = (Html.toHtml(t)).replace("<p>", "").replace(
"</p>", "");
// String chatStr =("CCC" + ":\n\n\t" +
// Html.toHtml(t)).replace("<p>", "").replace("</p>", "");
Log.v("chatStr==", chatStr);
CharSequence cs = Html.fromHtml(chatStr, imageGetter, null);
showText.append(cs);
showText.append("\n");
edit.setText("");
break;
case R.id.back_btn:// 取消
finish();
break;
default:
break;
}
}
/*
* 实例化一个ImageGetter用于下面的EditText显示表情时Html的转换 edittext里面的html标签替换为图片的一个方法
*/
ImageGetter imageGetter = new ImageGetter() {
@Overridehttp://www.huiyi8.com/qqbq/
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
// 根据id从资源文件中获取图片对象
Drawable d = getResources().getDrawable(id);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
}[/mw_shl_code]