最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程開發(fā)之EditText實(shí)現(xiàn)輸入QQ表情圖像的方法

 更新時間:2015年12月28日 15:42:49   作者:sgx425021234  
這篇文章主要介紹了Android編程開發(fā)之EditText實(shí)現(xiàn)輸入QQ表情圖像的方法,涉及Android多媒體文件及EditText的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程開發(fā)之EditText實(shí)現(xiàn)輸入QQ表情圖像的方法。分享給大家供大家參考,具體如下:

實(shí)現(xiàn)效果如下:

將QQ表情圖像放到res下的drawable-hdpi文件夾下:

布局文件:

<EditText
  android:id="@+id/edittext"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:inputType="text">
 </EditText>
 <Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/edittext"
  android:layout_alignRight="@+id/edittext"
  android:layout_below="@+id/edittext"
  android:layout_marginTop="38dp"
  android:text="添加QQ表情" />

MainActivity.java:

package com.example.edittext1;
import java.lang.reflect.Field;
import java.util.Random;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
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;
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) findViewById(R.id.edittext);
  button=(Button) findViewById(R.id.button1);
  //為按鈕注冊點(diǎn)擊事件
  button.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //產(chǎn)生隨機(jī)數(shù) 隨機(jī)數(shù)是從0開始,所以要加1,這樣就會產(chǎn)生1到9的隨機(jī)數(shù)
    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 imageSpan = new ImageSpan(MainActivity.this, bitmap);
     SpannableString spannableString = new SpannableString("face");//face就是圖片的前綴名
     spannableString.setSpan(imageSpan, 0, 4,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     editText.append(spannableString);
    } catch ( Exception e) {
    }
   }
  });
 }
 @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;
 }
}

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

佛坪县| 永顺县| 弋阳县| 定州市| 仁化县| 酉阳| 福清市| 绥江县| 滦南县| 吉林市| 淮滨县| 济阳县| 双鸭山市| 隆昌县| 罗定市| 阳新县| 黄石市| 方城县| 齐河县| 呼玛县| 桐庐县| 永年县| 台东县| 碌曲县| 海阳市| 车险| 泰来县| 大同县| 乌拉特前旗| 太谷县| 墨玉县| 扬中市| 伊吾县| 广元市| 江西省| 古交市| 扎囊县| 平度市| 班玛县| 洛阳市| 昌江|