Android switches languages in the app

Source: Internet
Author: User

scene: Switch between English and Chinese in settings, then jump to mainactivity .... Steps:1 . Initialize the language String savedlanguage in MyApplication =languageutil.getsavedlanguage ();if(Textutils.isempty (savedlanguage)) {Languageutil.setlanguage ("cn");//set the default language to "CN"}2 in Mainactiity @Override onnewintent () This method, if not overridden, does not refresh when launch mode is a single task @Overrideprotected voidonnewintent (Intent Intent) {if(intent.getaction () = = "Language Changed") {finish (); StartActivity (NewIntent ( This, Mainactivity.class)); } Else {        Super. Onnewintent (Intent); }} in Mainactiity @Override onnewintent () This method, if not overridden, does not refresh when launch mode is a single task3. Setting the interface code and layout file Public classSettingactivityextendsbaseactivity {radiogroup rglanguage;    RadioButton RBCN;    RadioButton Rben; Private intCheckedradiobuttonid; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_second); Rglanguage=Super.$( This, R.id.rglanguage); RBCN=Super.$( This, R.ID.RBCN); Rben=Super.$( This, R.id.rben); if("en". Equals (Languageutil.getsavedlanguage ())) {rben.setchecked (true); } Else{rbcn.setchecked (true); } Checkedradiobuttonid=Rglanguage.getcheckedradiobuttonid (); Super.$( This, r.id.btnconfirm). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(Checkedradiobuttonid! =Rglanguage.getcheckedradiobuttonid ()) {                    if(rbcn.ischecked ()) {Languageutil.setlanguage ("CN"); } Else if(rben.ischecked ()) {Languageutil.setlanguage ("EN");                    } finish (); Intent Intent=NewIntent (Myapplication.getcontext (), firstactivity.class); Intent.setaction ("Language Changed");                StartActivity (Intent); } Else{finish ();    }            }        }); }} set the XML layout of the interface<?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" > <TextView android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "@string/switcher"/> <Radiogroup Android:id= "@+id/rglanguage"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Vertical" > <RadioButton Android:id= "@+id/rbcn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Chinese"/> <RadioButton Android:id= "@+id/rben"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "中文版"/> </RadioGroup> <Button Android:id= "@+id/btnconfirm"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "@string/confirm"/></linearlayout>4.LanguageUtil Code Public classLanguageutil {/*** Set Language * *@paramlanguage "cn" = Chinese, "en" = English*/     Public Static voidSetLanguage (String language) {Resources Resources=Myapplication.getcontext (). Getresources (); Configuration Configuration=resources.getconfiguration (); Configuration.locale=NewLocale (language); Resources.updateconfiguration (Configuration,NULL);    Savelanguage (language); }     Public Static voidSavelanguage (String language) {Myapplication.getcontext (). Getsharedpreferences ("Settings", Context.mode_private). Edit (). putstring ("Language", language). commit (); }     Public StaticString getsavedlanguage () {returnMyapplication.getcontext (). Getsharedpreferences ("Settings", Context.mode_private). GetString ("Language",NULL); }}

Android switches languages in 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.