Android development skills like QQ input text and expression image _android

Source: Internet
Author: User
Tags mixed
EditText and TextView, can also be mixed row and text. The difference is that TextView is only used to display the graphics and text mixed row effect, and edittext can not only display, but also mixed input text and images, let us review the QQ chat input box shown in Figure 5.2, in the input box can be entered at the same time text and expression images. In fact, this effect can only be achieved in a few lines of code in the Android SDK. To make the reader more impulsive in learning, first come to appreciate the effect that is about to be achieved, as shown in Figure 5.16.

Figure 5.16 Entering text and images in the EditText control
To implement this program, first prepare some material to use, which is the image file to be entered in the EditText control. This example prepares 9 PNG image files (face1.png to Face9.png) and is placed in the Res\drawable directory.
Next, put a edittext and a button on the screen that can only display 3 lines (you can enter multiple lines), and the layout file has the following code:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "vertical" android:layout_width= "fill_parent"
android:layout_height= "Fill_parent" >
<edittext android:id= "@+id/edittext" android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content" android:lines= "3" android:gravity= "Left|top"/>
<button android:layout_width= "Wrap_content"
android:layout_height= "wrap_content" android:text= "random Insert Expression"
android:onclick= "Onclick_randomface" android:layout_margintop= "10DP"/>
</LinearLayout>

The Android:gravity property value is set to Left|top in the <EditText> tag of the above code. So that the text you enter appears in the upper-left corner, and if you do not set this property, the text you enter starts at the center of the left (because android:line= "3" is set, so that EditText can display the contents of three rows at the same time, so this problem occurs if you show only one row, There is no such problem).

The <Button> tag's Android:onclick property value specifies the Click event Method (Onclick_randomface), in which the ID of any image resource in face1.png to Face9.png is randomly obtained. The most common method is to place the 9 image resource IDs in the array and randomly generate an array index to get the corresponding image resource ID. But this example does not adopt this method, but uses the method that obtains the image resource ID from the R.drawable class directly through the reflection technology. The advantage of this approach is that the image resource is very long, and you can get any image resource ID without having to define it in the array.

The tab is used in 5.2.2 to insert images, although this is also the way to insert images in EditText controls. But this example uses another, simpler way of using the Android.text.style.ImageSpan class to insert the image directly. Let's look at the specific implementation code below.
Copy Code code as follows:

public void Onclick_randomface (view view)
{
///1 to 9 random integer
in T Randomid = 1 + new Random (). Nextint (9);
Try
{
//= Field object for the corresponding resource ID (static variable) from the R.drawable class from the randomly generated 1 to 9 integer
Field field = r.drawable.class.getdeclared Field ("face" + randomid);
//Gets the value of the resource ID, which is the value of the static variable
int ResourceID = integer.parseint (Field.get (null). ToString ());
//Bitmap object for resource image based on resource ID
Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), ResourceID);
//Create Imagespan object based on bitmap object
Imagespan imagespan = new Imagespan (this, bitmap);
//Create a Spannablestring object to insert an image encapsulated with a Imagespan object
spannablestring spannablestring = new Spannablestring ("Face") ;
//Replace face with Imagespan object
Spannablestring.setspan (Imagespan, 0, 4, spannable.span_exclusive_exclusive);
//Append the randomly obtained image to the last
Edittext.append (spannablestring) of the EditText control;
}
catch (Exception e)
{
}
}

writing the above code requires attention to the following points
1. Because the resource IDs in r.drawable are static variables that are public, the values of these variables can be obtained directly using the Field.get method. If you are a private or protected variable, you need Field.setaccessible (true) to set the access rights of the variable values to read and write the variables.
2. When you use the Field.get method to obtain a variable value, if it is a static variable. The parameter value of the Field.get method is set to null. If it is not a static variable, you need to specify the object of the class that contains the variable as the parameter value for the Field.get method.
3. Because the EditText class cannot be inserted directly into a span object, you need to first encapsulate the span object (such as the Imagespan object in this example) by using the Spannablestring object. The Spannablestring object is then inserted into the EditText control.
Related Article

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.