How does android add an emoticon on textview or edittext? textviewedittext

Source: Internet
Author: User

How does android add an emoticon on textview or edittext? textviewedittext

First:



First, write a GridView

Public class EmotionView extends LinearLayout implements OnItemClickListener {private GridView mGridView; private static final ArrayList <Integer> emotionDisplayList = new ArrayList <Integer> (); public static final LinkedHashMap <Integer, string> emotionsKeySrc = new LinkedHashMap <Integer, String> (); public static final HashMap <String, Integer> emotionsKeyString = new HashMap <String, Integer> (); private c Lass GridViewAdapter extends BaseAdapter {List <Integer> list; public GridViewAdapter (List <Integer> list) {super (); this. list = list;} public int getCount () {return list. size ();} public Object getItem (int position) {return list. get (position);} public long getItemId (int position) {return position;} public View getView (int position, View convertView, ViewGroup parent) {int resId = (Integer) getItem (positio N); ImageView iv = null; if (convertView = null) {iv = (ImageView) (convertView = new ImageView (getContext (). getApplicationContext ();} else {iv = (ImageView) convertView;} iv. setImageResource (resId); iv. setBackgroundResource (R. drawable. bg_face); int height = getResources (). getDimensionPixelSize (R. dimen. emotion_item_view_height); iv. setPadding (0, height, 0, height); return iv ;}} static {emotionsKeySrc. pu T (R. drawable. face1, "Angry"); emotionsKeySrc. put (R. drawable. face2, "cake"); emotionsKeySrc. put (R. drawable. face3, "Candle"); emotionsKeySrc. put (R. drawable. face4, "Cheers"); emotionsKeySrc. put (R. drawable. face5, "crazy"); emotionsKeySrc. put (R. drawable. face6, "fading"); emotionsKeySrc. put (R. drawable. face7, "Dizzy"); emotionsKeySrc. put (R. drawable. face8, "zongzi"); emotionsKeySrc. put (R. drawable. face9, "fan"); emotionsKeySrc. put (R. drawable. fac E10, "Flower"); emotionsKeySrc. put (R. drawable. face11, "soccer"); emotionsKeySrc. put (R. drawable. face12, "Green Ribbon"); emotionsKeySrc. put (R. drawable. face13, "hum"); emotionsKeySrc. put (R. drawable. face14, "heart"); emotionsKeySrc. put (R. drawable. face15, "Popsicle"); emotionsKeySrc. put (R. drawable. face16, "Haha"); emotionsKeySrc. put (R. drawable. face17, "love you"); emotionsKeySrc. put (R. drawable. face18, "moon"); emotionsKeySrc. put (R. drawable. face19 ," Pig "); emotionsKeySrc. put (R. drawable. face20, "Rain"); emotionsKeySrc. put (R. drawable. face21, "Red Card"); emotionsKeySrc. put (R. drawable. face22, "Tears"); emotionsKeySrc. put (R. drawable. face23, "Whistle"); emotionsKeySrc. put (R. drawable. face24, "sleepy"); emotionsKeySrc. put (R. drawable. face25, "Haha"); emotionsKeySrc. put (R. drawable. face26, "Sunshine"); emotionsKeySrc. put (R. drawable. face27, "Khan"); emotionsKeySrc. put (R. drawable. face28, "Yellow Card"); em OtionsKeySrc. put (R. drawable. face29, ""); emotionsKeySrc. put (R. drawable. face30, "sad"); Iterator <Integer> iterator = emotionsKeySrc. keySet (). iterator (); int I = 0; while (iterator. hasNext () {int temp = iterator. next (); emotionsKeyString. put (emotionsKeySrc. get (temp), temp); emotionDisplayList. add (temp); I ++ ;}} public interface EmotionAdapter {void doAction (int resId, String desc);} public EmotionView (Conte Xt context) {super (context); initViews ();} public EmotionView (Context context, AttributeSet attrs) {super (context, attrs); initViews ();} private void initViews () {Context context = getContext (); LayoutInflater. from (context ). inflate (R. layout. emotion_main, this); mGridView = (GridView) findViewById (R. id. gridView); mGridView. setAdapter (new GridViewAdapter (emotionDisplayList); mGridView. setOnItemClickListene R (this) ;}@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3) {int value = emotionDisplayList. get (position); listener. onclick (value);} MyListener listener; public void setListener (MyListener listener) {this. listener = listener ;}}

Layout is:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="@dimen/emotion_view_height"android:background="#d1d8e3" ><GridViewandroid:id="@+id/gridView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:numColumns="@integer/emotion_colnum"android:cacheColorHint="@null"android:stretchMode="columnWidth"android:listSelector="#00000000"android:fadingEdgeLength="0dp" ></GridView></RelativeLayout>


<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"    tools:context=".MainActivity" >   <com.example.weibotest.view.EmotionViewandroid:id="@+id/emotion_view"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_centerInParent="true"/>      <EditText        android:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:textSize="18sp"android:textColor="#000000"android:layout_marginBottom="10dp"android:layout_above="@id/emotion_view"       /></RelativeLayout>

Write in the Activity as follows:

EmotionView mEmotionView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final TextView textView = (TextView) findViewById (R. id. textview); mEmotionView = (EmotionView) findViewById (R. id. emotion_view); textView. setText ("<strong> a [dizzy] dfsfdf [sunlight] ds </strong>"); String str1 = textView. getText (). toString (); SpannableString s PannableString = new SpannableString (str1); Pattern pattern = null; <strong> pattern = Pattern. compile ("\ [(\ S + ?) \] "); // Here is the string in the form of [XX, replace the string in this form with the corresponding expression </strong> Matcher matcher = pattern. matcher (str1); Integer drawableSrc = null; while (matcher. find () {int start = matcher. start (); int end = matcher. end (); drawableSrc = EmotionView. emotionsKeyString. get <strong> (matcher. group (1) </strong>; if (drawableSrc! = Null & drawableSrc> 0) {<strong> spannableString. setSpan (new ImageSpan (MainActivity. this, drawableSrc), start, end, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); </strong >}} textView. setText (spannableString, TextView. bufferType. SPANNABLE); mEmotionView. setListener (new MyListener () {@ Overridepublic void onclick (int value) {String str1 = textView. getText (). toString (); SpannableString str = new SpannableString (str1); if (value> 0) {str. setSpan (new ImageSpan (MainActivity. this, value), 4, 5, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); textView. setText (str, TextView. bufferType. SPANNABLE );}}});


Code: http://download.csdn.net/detail/baidu_nod/7697419


Can android add Textview and Edittext to Imageview?

Replace simpleAdapter with baseAdapter
// Clear:
List = null;
Adapter. yydatachanged ();
// Add:
List = value you want to assign;
Adapter. yydatachanged ();
Ps: Why are you changing the name of the arrayAdapter and simpleAdapter instance?


In Android development, how does one click the Button to display the content in EditText in textView?

Public class MainActivity extends Activity implements View. onClickListener {private EditText editText; private TextView textView; public void onCreate (Bundle saveInstanceState) {super. onCreate (saveInstanceState); findViewById (R. id. button ). setOnClickListener (this); editText = (EditText) findViewById (R. id. editText); textView = (TextView) findViewById (R. id. textView);} public void onClick (View v ){ TextView. setText (editText. getText ());//! } As long as EditText and TextView have a Button in the layout.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.