Read "Android from getting Started to mastering" (30)--fonts

Source: Internet
Author: User

TypeFace class method


TypeFace of five fonts and four font types


TypeFace Example

Complete project:

1.mainactivity.java

Package Com.sweetlover.activity;import Com.sweetlover.typeface.r;import Android.app.activity;import Android.graphics.color;import Android.graphics.typeface;import Android.os.bundle;import Android.widget.EditText; Import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;import Android.widget.textview;import Android.widget.Toast; public class Mainactivity extends Activity {private EditText EditText = null;private Radiogroup Radiogroup = null;private radiobutton[] radiobtn = null;private TextView TextView = null; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); editText = (EditText) Findviewbyid (R.ID.EDITTEXT1); radiogroup = (Radiogroup) Findviewbyid ( R.ID.RG) radiobtn = new Radiobutton[5];radiobtn[0] = (RadioButton) Findviewbyid (R.ID.RB1); radiobtn[1] = (RadioButton) Findviewbyid (R.ID.RB2); radiobtn[2] = (RadioButton) Findviewbyid (R.ID.RB3); radiobtn[3] = (RadioButton) Findviewbyid (R.ID.RB4); radiobtn[4] = (RadioButton) Findviewbyid (r.id.rb5); TextView = (TextView) Findviewbyid (R.id.textview); Radiogroup.setoncheckedchangelistener ( New Checkchangedlistener ());} Class Checkchangedlistener implements Oncheckedchangelistener {@Overridepublic void oncheckedchanged (Radiogroup group , int checkedid) {//TODO auto-generated method stubif (Checkedid = = Radiobtn[0].getid () && radiobtn[0].ischecked ( ) {Textview.settext (Edittext.gettext ()); Textview.settextcolor (Color.Blue); Textview.settypeface ( Typeface.default, Typeface.normal); Toast.maketext (Mainactivity.this,radiobtn[0].gettext () + "selected", Toast.length_short). Show (); else if (Checkedid = = Radiobtn[1].getid () && radiobtn[1].ischecked ()) {Textview.settext (Edittext.gettext ()); Textview.settextcolor (Color.Blue); Textview.settypeface (Typeface.default_bold, typeface.bold); Toast.maketext (Mainactivity.this,radiobtn[1].gettext () + "selected", Toast.length_short). Show ();} else if (Checkedid = = Radiobtn[2].getid () && radiobtn[2].ischecked ()) {Textview.settext (Edittext.gettext ()); Textview.settextcolor (Color.Blue); Textview.settypeface (Typeface.monospace, typeface.italic); Toast.maketext (Mainactivity.this,radiobtn[2].gettext () + "selected", Toast.length_short). Show (); else if (Checkedid = = Radiobtn[3].getid () && radiobtn[3].ischecked ()) {Textview.settext (Edittext.gettext ()); Textview.settextcolor (Color.Blue); Textview.settypeface (Typeface.monospace, typeface.bold_italic); Toast.maketext (Mainactivity.this,radiobtn[3].gettext () + "selected", Toast.length_short). Show (); else if (Checkedid = = Radiobtn[4].getid () && radiobtn[4].ischecked ()) {Textview.settext (Edittext.gettext ()); Textview.settextcolor (Color.Blue); Textview.settypeface (Typeface.default_bold, Typeface.bold); TextView.getPaint ( ). Setfakeboldtext (True); Toast.maketext (Mainactivity.this,radiobtn[4].gettext () + "selected", Toast.length_short). Show ();}}}

2.activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <edittext android:id= "@+id/edittext1" android:layout_width= "300DP" Android:layout_heig        ht= "60DP" android:layout_gravity= "center" android:textsize= "30SP" android:hint= "@string/note"/> <radiogroup android:id= "@+id/rg" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_gravity=" center "android:orientation=" vertical "> <radiobu Tton android:id= "@+id/rb1" android:layout_width= "wrap_content" android:layout_height= "Wrap_co            Ntent "android:text=" @string/rb1 "/> <radiobutton android:id=" @+id/rb2 " Android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content" android:text= "@string/rb2"/> <radiobutton        Android:id= "@+id/rb3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/rb3"/> <radiobutton android:id= "@+id/rb4" Android:lay                Out_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/rb4"/> <radiobutton android:id= "@+id/rb5" android:layout_width= "Wrap_content" android:layout_he ight= "Wrap_content" android:text= "@string/rb5"/> </RadioGroup> <textview Android Oid:id= "@+id/textview" android:layout_width= "300DP" android:layout_height= "60DP" Android:layout_gravi ty= "center" android:textsize= "30SP" android:text= "@string/note"/></linearlayout>

3.string.xml

<resources>    <string name= "app_name" >TypeFace</string>    <string name= "Note" > Please enter text </string>    <string name= "RB1" > Default </string>    <string name= "Rb2" > Bold </string>    <string name= "Rb3" > Italic </string>    <string name= "Rb4" > Bold italic </string>    <string Name= "Rb5" > Faux bold </string></resources>

Read "Android from getting Started to mastering" (30)--fonts

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.