Android 系统语言切换

只是进项简单的手动切换方法,希望有所帮助。

前面的String.xml和一些布局文件就略过了。直接上主代码:

package info.btsland.app.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import info.btsland.app.R;
import info.btsland.app.ui.fragment.HeadFragment;
import info.btsland.app.util.PreferenceUtil;


public class SettingActivity extends BaseActivity {

    private TextView tvSetLanguage;
    private TextView tvSetTheme;
    private TextView tvSetGuide;
    private TextView tvSetWe;
    private TextView tvSetEdition;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(info.btsland.app.R.layout.activity_setting);
        fillInHead();
        init();
}

    /**
     * 初始化
     */
    private void init(){
        tvSetLanguage= (TextView) findViewById(R.id.tv_set_language);
        tvSetTheme= (TextView) findViewById(R.id.tv_set_theme);
        tvSetGuide= (TextView) findViewById(R.id.tv_set_guide);
        tvSetWe= (TextView) findViewById(R.id.tv_set_we);
        tvSetEdition= (TextView) findViewById(R.id.tv_set_edition);

        //绑定特效事件
        TextViewOnTouchListener OnTouchListener=new TextViewOnTouchListener();
        tvSetLanguage.setOnTouchListener(OnTouchListener);
        tvSetTheme.setOnTouchListener(OnTouchListener);
        tvSetGuide.setOnTouchListener(OnTouchListener);
        tvSetWe.setOnTouchListener(OnTouchListener);
        tvSetEdition.setOnTouchListener(OnTouchListener);

        //绑定点击事件
        TextViewOnClickListener OnClickListener=new TextViewOnClickListener();
        tvSetLanguage.setOnClickListener(OnClickListener);

        //判断checkedItem的值
        String lo = PreferenceUtil.getString("language", "zh");
        Log.i("init", "init: "+lo);
        if (lo.equals("zh")){
            index=0;
        }else if (lo.equals("en")){
            index=1;
        }
        Log.i("init", "init: "+index);
    }

    /*
     *点击事件
     */
    class TextViewOnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_set_language:
                    showListDialog();
                    break;
                case R.id.tv_set_theme:
                    break;
            }
        }
    }

    /**
     * 跳出Dialog窗口
     */

    int index = 0 ;//设置默认选项,作为checkedItem参数传入。

    private void showListDialog() {

        AlertDialog.Builder listDialog = new AlertDialog.Builder(SettingActivity.this);
        listDialog.setTitle(getString(R.string.selectlanguage));

        listDialog.setIcon(android.R.drawable.ic_dialog_info);

        final String[] items={"中文","英文"};
        items[0]=getString(R.string.stringzh);
        items[1]=getString(R.string.stringen);


        listDialog.setSingleChoiceItems(items, index, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(items[which]==getString(R.string.stringzh)){
                    switchLanguage("zh");
                }else if(items[which]==getString(R.string.stringen)){
                    switchLanguage("en");
                }
                dialog.dismiss();
                finish();

                Intent intent=new Intent(SettingActivity.this,MainActivity.class);
                //开始新的activity同时移除之前所有的activity
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            }
        });
        listDialog.show();
    }


    /**
     * 单击特效
     * @param textView 被单击的tv
     * @param motionEvent 当前状态
     */
    protected void touchColor(TextView textView,MotionEvent motionEvent){
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            textView.setBackground(getResources().getDrawable(R.drawable.tv_row_touch,null));
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            textView.setBackground(getResources().getDrawable(R.drawable.tv_row,null));
        }
    }
    class TextViewOnTouchListener implements View.OnTouchListener{
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (view.getId()) {
                case R.id.tv_set_language:
                    touchColor(tvSetLanguage,motionEvent);
                    break;
                case R.id.tv_set_theme:
                    touchColor(tvSetTheme,motionEvent);
                    break;
            }
            return false;
        }
    }
}



BaseActivity类:
package info.btsland.app.ui.activity;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;

import java.util.Locale;

import info.btsland.app.util.PreferenceUtil;

/**
 * Created by Administrator on 2017/10/13 0013.
 */

public class BaseActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * <切换语言>
     *
     * @param language
     */

    protected void switchLanguage(String language) {
        // 设置应用语言类型
        Resources 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);

        // 保存设置语言的类型
        PreferenceUtil.commitString("language",language);

    }
}


MainActivity类:
package info.btsland.app.ui.activity;


import info.btsland.app.util.PreferenceUtil;


public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化PreferenceUtil
        PreferenceUtil.init(this);
        // 依据上次的语言设置,又一次设置语言
        switchLanguage(PreferenceUtil.getString("language", "zh"));
        setContentView(R.layout.activity_main);
        fillInHead();
    }


工具类PreferenceUtil:
package info.btsland.app.util;

import android.content.Context;
import android.content.SharedPreferences;


public class PreferenceUtil {
    private static SharedPreferences mSharedPreferences = null;
    private static SharedPreferences.Editor mEditor = null;

    public static void init(Context context){
        if (null == mSharedPreferences) {
            mSharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context) ;
        }
    }

    public static void removeKey(String key){
        mEditor = mSharedPreferences.edit();
        mEditor.remove(key);
        mEditor.commit();
    }

    public static void removeAll(){
        mEditor = mSharedPreferences.edit();
        mEditor.clear();
        mEditor.commit();
    }

    public static void commitString(String key, String value){
        mEditor = mSharedPreferences.edit();
        mEditor.putString(key, value);
        mEditor.commit();
    }

    public static String getString(String key, String faillValue){
        return mSharedPreferences.getString(key, faillValue);
    }

    public static void commitInt(String key, int value){
        mEditor = mSharedPreferences.edit();
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    public static int getInt(String key, int failValue){
        return mSharedPreferences.getInt(key, failValue);
    }

    public static void commitLong(String key, long value){
        mEditor = mSharedPreferences.edit();
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    public static long getLong(String key, long failValue) {
        return mSharedPreferences.getLong(key, failValue);
    }

    public static void commitBoolean(String key, boolean value){
        mEditor = mSharedPreferences.edit();
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    public static Boolean getBoolean(String key, boolean failValue){
        return mSharedPreferences.getBoolean(key, failValue);
    }

}


SettingActivity的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_Grey"
tools:context="info.btsland.app.ui.activity.UserActivity">


    <View
        android:id="@+id/v_set_back"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:background="@drawable/welcome2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fra_set_head"
        app:layout_constraintHorizontal_bias="0.0" />

    <TextView
        android:id="@+id/tv_set_language"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:text="@string/language"
        android:gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/tv_row"
        android:textColor="@color/color_white"
        app:layout_constraintTop_toBottomOf="@+id/v_set_back"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="5dp" />
    <TextView
        android:id="@+id/tv_set_theme"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:text="@string/theme"
        android:gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/tv_row"
        android:textColor="@color/color_white"
        app:layout_constraintTop_toBottomOf="@+id/tv_set_language"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

猜你喜欢

转载自wenweijie.iteye.com/blog/2397098