Android 语言切换功能

最近项目要实现语言切换功能,大概实现了一下,先上效果图:

这里主要是通过点击语言切换弹出Popwindow进行语言选择
英文状态下
中文状态下

记下来主要是为了以后方便查看,下面来看下主要实现步骤。

先在res资源文件下新建两个文件夹,values-zh,values-en,然后文件夹下新建各建一个strings.xml文件,如果要实现其他语言操作步骤一样,项目里只用到中文和英文,所以就实现了这两种语言,实际文件资源视图:
AS下视图结构
由于英语水平有限借助了谷歌翻译。。。

接下来就是主要代码:
1,Dialog主要代码

public class DialogLanguage {
    private Dialog mDialog;
    private int mLanguage;
    private static DialogLanguage instance;

    public static DialogLanguage getInstance() {
        if (instance == null) {
            synchronized (CustomDialog.class) {
                if (null == instance) {
                    instance = new DialogLanguage();
                }
            }
        }
        return instance;
    }

    public void ShowLanguageDialog(final Context context, int type, final LanguageListener callBack) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_language_select, null);
        RadioGroup rg = (RadioGroup) view.findViewById(R.id.radioGroup);
        final RadioButton ch = (RadioButton) view.findViewById(R.id.radioButtonCh);
        final RadioButton en = (RadioButton) view.findViewById(R.id.radioButtonEn);
        switch (type) {
            case 1:
                ch.setChecked(true);
                en.setChecked(false);
                ch.setTextColor(context.getResources().getColor(R.color.colorThem));
                en.setTextColor(context.getResources().getColor(R.color.textDark));
                break;
            case 2:
                en.setChecked(true);
                ch.setChecked(false);
                ch.setTextColor(context.getResources().getColor(R.color.textDark));
                en.setTextColor(context.getResources().getColor(R.color.colorThem));
                break;
            default:
                break;
        }
        mLanguage = 1;
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radioButtonCh:
                        mLanguage = 1;
                        ch.setTextColor(context.getResources().getColor(R.color.colorThem));
                        en.setTextColor(context.getResources().getColor(R.color.textDark));
                        break;
                    case R.id.radioButtonEn:
                        ch.setTextColor(context.getResources().getColor(R.color.textDark));
                        en.setTextColor(context.getResources().getColor(R.color.colorThem));
                        mLanguage = 2;
                        break;
                }
            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.tv_confirm:
                        callBack.languageListener(mLanguage);
                        mDialog.dismiss();
                        break;
                }
            }
        };

        view.findViewById(R.id.tv_confirm).setOnClickListener(listener);
        mDialog = new Dialog(context, R.style.MyDialogStyle);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return true;
            }
        });
        mDialog.setContentView(view);
        Window window = mDialog.getWindow();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = ScreenUtils.getScreenWidth(context) - DensityUtils.dp2px(context, 70f);
        window.setAttributes(p);
        mDialog.show();
    }

    public interface LanguageListener {
        void languageListener(int number);
    }
}

2,布局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="wrap_content"
    android:background="@color/dialog_bg"
    android:padding="@dimen/activity_margin_20"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/activity_title_height"
        android:text="@string/language"
        android:gravity="center"
        android:textColor="@color/textMiddleDark"
        android:textSize="@dimen/activity_size_them"/>

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/radioButtonCh"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/CHINESE"
            android:textSize="@dimen/size_16"
            android:background="@null"
            android:layout_marginTop="@dimen/activity_margin_15"
            android:button="@null"
            android:textColor="@color/textDark"
            android:drawablePadding="0dp"
            android:gravity="center"/>

        <RadioButton
            android:id="@+id/radioButtonEn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/ENGLISH"
            android:layout_marginTop="@dimen/activity_margin_20"
            android:textSize="@dimen/size_16"
            android:background="@null"
            android:button="@null"
            android:textColor="@color/textDark"
            android:drawablePadding="0dp"
            android:gravity="center"/>
    </RadioGroup>

    <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/confirm"
        android:textColor="@color/colorThem"
        android:textSize="@dimen/activity_size_them"
        android:layout_marginTop="@dimen/activity_margin_25"/>

</LinearLayout>

3,调用Dialog部分

SharedPreferences sp = getSharedPreferences("language", MODE_PRIVATE);
                int type = sp.getInt("language", 0);
                DialogLanguage.getInstance().ShowLanguageDialog(this, type, new DialogLanguage.LanguageListener() {
                    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
                    @Override
                    public void languageListener(int num) {
                        //将选中项存入SharedPreferences,以便重启应用后读取设置
                        SharedPreferences preferences = getSharedPreferences("language", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putInt("language", num);
                        editor.apply();
                       /* Intent intent = new Intent(Login.this, Login.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);*/
                        Resources resources = getResources();
                        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
                        Configuration configuration = resources.getConfiguration();
                        switch (num) {
                            case 1:
                                configuration.locale = Locale.SIMPLIFIED_CHINESE;
                                break;
                            case 2:
                                configuration.locale = Locale.ENGLISH;
                                break;
                            default:
                                break;
                        }

                        resources.updateConfiguration(configuration, displayMetrics);
                        Login.this.finish();
                        //重新在新的任务栈开启新应用
                        App.getInstance().finishAllActivity();
                        Intent intent = new Intent(Login.this, Login.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                        // android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });

4,在启动页读取已保存的语言种类,然后进行设置

  private void setLanguage() {
        //读取SharedPreferences数据,默认选中第一项
        SharedPreferences preferences = getSharedPreferences("language", Context.MODE_PRIVATE);
        int language = preferences.getInt("language", 1);

        //根据读取到的数据,进行设置
        Resources resources = getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        switch (language) {
            case 1:
                configuration.locale = Locale.SIMPLIFIED_CHINESE;
                break;
            case 2:
                configuration.locale = Locale.ENGLISH;
                break;
            default:
                break;
        }

        resources.updateConfiguration(configuration, displayMetrics);
        //setLanguage();

    }

至此已经完成了中英文语言切换功能。

猜你喜欢

转载自blog.csdn.net/zhuhuitao_struggle/article/details/75307507