Android Multi-language (switch languages within the app)

Source: Internet
Author: User

To create a Sharedpreferences management class

 Public classPreferenceutil {Private StaticSharedpreferences msharedpreferences =NULL; Private StaticEditor Meditor =NULL;  Public Static voidinit (Context context) {if(NULL==msharedpreferences) {msharedpreferences=android.preference.PreferenceManager.getDefaultSharedPreferences (context); }    }         Public Static voidRemoveKey (String key) {Meditor=Msharedpreferences.edit ();        Meditor.remove (key);    Meditor.commit (); }         Public Static voidRemoveAll () {Meditor=Msharedpreferences.edit ();        Meditor.clear ();    Meditor.commit (); }     Public Static voidcommitstring (string key, String value) {Meditor=Msharedpreferences.edit ();        Meditor.putstring (key, value);    Meditor.commit (); }         Public Staticstring getString (String key, String faillvalue) {returnmsharedpreferences.getstring (key, Faillvalue); }         Public Static voidCommitint (String Key,intvalue) {Meditor=Msharedpreferences.edit ();        Meditor.putint (key, value);    Meditor.commit (); }         Public Static intGetInt (String Key,intfailvalue) {        returnmsharedpreferences.getint (key, Failvalue); }         Public Static voidCommitlong (String Key,Longvalue) {Meditor=Msharedpreferences.edit ();        Meditor.putlong (key, value);    Meditor.commit (); }         Public Static LongGetlong (String Key,Longfailvalue) {        returnMsharedpreferences.getlong (key, Failvalue); }         Public Static voidCommitboolean (String Key,Booleanvalue) {Meditor=Msharedpreferences.edit ();        Meditor.putboolean (key, value);    Meditor.commit (); }         Public StaticBoolean Getboolean (String key,Booleanfailvalue) {        returnMsharedpreferences.getboolean (key, Failvalue); }}

Creating the Baseactivity Class

 Public classBaseactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Initialize PreferenceutilPreferenceutil.init ( This); //Reset the language based on the last language settingSwitchlanguage (preferenceutil.getstring ("language", "zh")); }            protected voidSwitchlanguage (String language) {//Set the application language typeResources Resources =getresources (); Configuration Config=resources.getconfiguration (); Displaymetrics DM=Resources.getdisplaymetrics (); if(Language.equals ("en")) ) {Config.locale=Locale.english; } Else{Config.locale=Locale.simplified_chinese;                } resources.updateconfiguration (config, DM); //Save the type of setting languagePreferenceutil.commitstring ("Language", language); }}

Mainactivity

 Public classMainactivityextendsBaseactivityImplementsonclicklistener{PrivateDialog Mdialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Displaymetrics DM=NewDisplaymetrics ();        Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); String Str= "Mobile Resolution:" +dm.widthpixels+ "X" +Dm.heightpixels; LOG.I (L, str); TextView TextView=(TextView) Findviewbyid (R.id.text); Button Button=(Button) Findviewbyid (R.ID.BTN); Button Button2=(Button) Findviewbyid (r.id.btn_2);        Textview.settext (R.string.hello_world);                Button.settext (R.string.switch_language); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(Mdialog = =NULL) {Layoutinflater inflater=Getlayoutinflater (); View Layout= Inflater.inflate (R.layout.dialog_select_lanuage,NULL); TextView 中文版=(TextView) Layout.findviewbyid (r.id.select_english); TextView Chinese=(TextView) Layout.findviewbyid (R.id.select_chinese); Mdialog=NewDialog (mainactivity. This, R.style.custom_dialog_theme); Mdialog.setcanceledontouchoutside (false); English.setonclicklistener (mainactivity. This); Chinese.setonclicklistener (mainactivity. This);                Mdialog.setcontentview (layout);            } mdialog.show ();                }        }); Button2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent it=NewIntent (mainactivity. This, Secondactivity.class);            StartActivity (IT);    }        }); } @Override Public voidOnClick (View v) {Mdialog.dismiss (); Switch(V.getid ()) { CaseR.id.select_english:switchlanguage ("EN");  Break;  CaseR.id.select_chinese:switchlanguage ("EN");  Break; default:                 Break; }        //after updating the language, destroy the current page, redrawfinish (); Intent it=NewIntent (mainactivity. This, Mainactivity.class);    StartActivity (IT); }}

  

Android Multi-language (switch languages within the app)

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.