"Original" uses typeface to implement different font invocation display and string conversion to Unicode

Source: Internet
Author: User

I've been working on it lately, just write a little demo.

The demo implementation uses typeface to invoke different fonts from assets and displays them in EditText

Create a activity_main.xml file in 1.layout

The layout code is as follows:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:background= "#ffffff"6 android:orientation= "vertical">7 8     <EditText9         Android:id= "@+id/edit_test1"Ten Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A Android:background= "#eeeeee" - android:focusable= "true" - Android:hint= "Font 1" /> the  -     <Button -         Android:id= "@+id/button1" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" - Android:layout_marginright= "39DP" + Android:layout_margintop= "14DP" A Android:text= "Font 2" /> at  -     <EditText -         Android:id= "@+id/edit_test2" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:layout_margintop= "50DP" in Android:background= "#eeeeee" - android:focusable= "true" to Android:hint= "Font 1" /> +  -     <Button the         Android:id= "@+id/button2" * Android:layout_width= "Wrap_content" $ Android:layout_height= "Wrap_content"Panax Notoginseng Android:layout_marginright= "39DP" - Android:layout_margintop= "14DP" the Android:text= "Font 2" /> +  A </LinearLayout>

The Java code is as follows:

Mainactivity.java

1  Packagecom.example.edittest;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Typeface;5 ImportAndroid.os.Bundle;6 Importandroid.view.KeyEvent;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.EditText; One ImportAndroid.widget.TextView; A ImportAndroid.widget.TextView.OnEditorActionListener; - ImportAndroid.widget.Toast; -  the  Public classMainactivityextendsActivity { -     PrivateEditText EditText1; -     PrivateEditText editText2; - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); A Initview (); at     } -  -      Public voidInitview () { -EditText1 =(EditText) Findviewbyid (r.id.edit_test1); -EDITTEXT2 =(EditText) Findviewbyid (R.ID.EDIT_TEST2); -  in         //Call Typeface's Createfromasset method here with two TTF files, Ziti1.ttf, Ziti2.ttf, here is getassets () this method -         //the font 1 path is the Fonts folder created under the assets directory:/assets/fonts/ziti1.ttf toTypeface Typeface1 =Typeface.createfromasset (Getassets (), +"Fonts/ziti1.ttf"); - Edittext1.settypeface (Typeface1); the  *         //Monitor Enter $Edittext1.setoneditoractionlistener (NewOneditoractionlistener () {Panax Notoginseng @Override -              Public BooleanOneditoraction (TextView V,intActionId, the KeyEvent Event) { +Toast.maketext (mainactivity. This, String.valueof (ActionId), A toast.length_short). Show (); the                 return false; +             } -         }); $         //the Font 2 path is the Fonts folder created under the assets directory:/assets/fonts/ziti2.ttf $Typeface Typeface2 =Typeface.createfromasset (Getassets (), -"Fonts/ziti2.ttf"); - Edittext2.settypeface (TYPEFACE2); the  -         //Monitor EnterWuyiEdittext2.setoneditoractionlistener (NewOneditoractionlistener () { the @Override -              Public BooleanOneditoraction (TextView V,intActionId, Wu KeyEvent Event) { -Toast.maketext (mainactivity. This, String.valueof (ActionId), About toast.length_short). Show (); $                 return false; -             } -         }); -  AButton getValue1 =(Button) Findviewbyid (r.id.button1); +Button getValue2 =(Button) Findviewbyid (r.id.button2); the  -Getvalue1.setonclicklistener (NewOnclicklistener () { $ @Override the              Public voidOnClick (View v) { the                 //Toast display Unicode code for character conversion theToast.maketext (mainactivity. This, the String2unicode (Edittext1.gettext (). toString ()), - toast.length_short). Show (); in             } the         }); theGetvalue2.setonclicklistener (NewOnclicklistener () { About @Override the              Public voidOnClick (View v) { the                 //Toast display Unicode code for character conversion theToast.maketext (mainactivity. This, + String2unicode (Edittext2.gettext (). toString ()), - toast.length_short). Show (); the             }Bayi         }); the     } the  -     //string converted to Unicode -      Public Staticstring String2unicode (String string) { theStringBuffer Unicode =NewStringBuffer (); the          for(inti = 0; I < string.length (); i++) { the             Charc =String.charat (i); theUnicode.append ("\\u" +integer.tohexstring (c)); -         } the         returnunicode.tostring (); the     } the}

Finally, after entering the characters in the EditText, click the button to display the corresponding Unicode code separately.

Diagram and demo I do not go, a very simple example, I hope to help the students in need.

"Original" uses typeface to implement different font invocation display and string conversion to Unicode

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.