Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中實現Edittext中輸入表情圖片的方法

Android中實現Edittext中輸入表情圖片的方法

編輯:關於Android編程

[java]
 



相關代碼如下:


[java]
package weibo.lixiaodaoaaa.ui; 
 
import java.lang.reflect.Field; 
import java.util.Random; 
 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.text.SpannableString; 
import android.text.style.ImageSpan; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
 
public class MainActivity extends Activity implements OnClickListener 

 
    /**
     * ******************騰訊微博: http://t.qq.com/lixiaodaoaaa ***************
     * ******************新浪微博:http://weibo.com/lixiaodaoaaa ***************
     * ******************by:lixiaodaoaaa ***********************
     */ 
 
    private Context context; 
    private Button btn1; 
    private EditText et1; 
 
    public int getRandomResourceId() 
    { 
        try 
        { 
            int randmId = new Random().nextInt(7) + 1; 
            Field field = R.drawable.class.getDeclaredField("face_0" + randmId + ""); 
            return field.getInt(null); 
        } catch (Exception e) 
        { 
            e.printStackTrace(); 
            System.out.println("error is here"); 
        } 
        return 0; 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        initAllDatas(); 
        for (int i = 0; i < 100; i++) 
        { 
            btn1.performClick(); 
        } 
    } 
 
    private void initAllDatas() 
    { 
        context = MainActivity.this; 
        btn1 = (Button) findViewById(R.id.btn1); 
        et1 = (EditText) findViewById(R.id.et1); 
        btn1.setOnClickListener(this); 
    } 
 
    @Override 
    public void onClick(View v) 
    { 
        switch (v.getId()) { 
        case R.id.btn1: 
            ImageSpan imageSpan = new ImageSpan(context, BitmapFactory.decodeResource(getResources(), 
                    getRandomResourceId())); 
            SpannableString spannableString = new SpannableString("face"); 
            spannableString.setSpan(imageSpan, 0, spannableString.length(), SpannableString.SPAN_MARK_MARK); 
            et1.append(spannableString); 
            break; 
        default: 
            break; 
        } 
    } 

package weibo.lixiaodaoaaa.ui;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener
{

 /**
  * ******************騰訊微博: http://t.qq.com/lixiaodaoaaa ***************
  * ******************新浪微博:http://weibo.com/lixiaodaoaaa ***************
  * ******************by:lixiaodaoaaa ***********************
  */

 private Context context;
 private Button btn1;
 private EditText et1;

 public int getRandomResourceId()
 {
  try
  {
   int randmId = new Random().nextInt(7) + 1;
   Field field = R.drawable.class.getDeclaredField("face_0" + randmId + "");
   return field.getInt(null);
  } catch (Exception e)
  {
   e.printStackTrace();
   System.out.println("error is here");
  }
  return 0;
 }
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initAllDatas();
  for (int i = 0; i < 100; i++)
  {
   btn1.performClick();
  }
 }

 private void initAllDatas()
 {
  context = MainActivity.this;
  btn1 = (Button) findViewById(R.id.btn1);
  et1 = (EditText) findViewById(R.id.et1);
  btn1.setOnClickListener(this);
 }

 @Override
 public void onClick(View v)
 {
  switch (v.getId()) {
  case R.id.btn1:
   ImageSpan imageSpan = new ImageSpan(context, BitmapFactory.decodeResource(getResources(),
     getRandomResourceId()));
   SpannableString spannableString = new SpannableString("face");
   spannableString.setSpan(imageSpan, 0, spannableString.length(), SpannableString.SPAN_MARK_MARK);
   et1.append(spannableString);
   break;
  default:
   break;
  }
 }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved