您好,登錄后才能下訂單哦!
MainActivity.java
package com.example.android_editext_1face; import java.lang.reflect.Field; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; 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) { // TODO Auto-generated method stub int randomId = 1 + new Random().nextInt(1); try { Toast.makeText(MainActivity.this, "Hello", 0); Field f = R.drawable.class.getDeclaredField("face" + randomId); int resourceId = Integer.parseInt(f.get(null).toString()); // 在android中要想顯示圖片信息,必須使得Bitmap的對(duì)象來(lái)裝載 Bitmap bitmap = BitmapFactory.decodeResource( getResources(), resourceId); // ImageSpan ImageSpan p_w_picpathspan = new ImageSpan(MainActivity.this, bitmap); SpannableString spannablestring = new SpannableString( "face"); spannablestring.setSpan(p_w_picpathspan, 0, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // 添加內(nèi)容到edittext中 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; } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/edittext" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="添加qq表情" android:layout_marginTop="50dp" /> </RelativeLayout>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。