Android 简单自定义对话框:语言切换

先看效果。。。

先准备好各种语言的strings.xml...

右键values--->New--->Values Resource File...

文件名为strings,下面点击Locale,在点那个两个大于号的>>按钮,选择一个语言。。。OK,最后把原来的默认strings.xml里面的东西复制过来,一个个翻译好!


还需要Dialog的layout文件。。。叫做dialog_language_layout.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:background="@drawable/layout_dialog_back"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/white">

        <TextView
            android:id="@+id/txt_dialog_language_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="TITLE"
            android:textAllCaps="false"
            android:textColor="@color/font_gray"
            android:textSize="22sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_below="@+id/txt_dialog_language_title"
            android:layout_marginEnd="15dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="5dp"
            android:background="@color/devider"></LinearLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radio_sim_chn"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:text="@string/radio_txt_sim_chn"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/radio_eng"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:text="@string/radio_txt_en"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/dialog_language_ok"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                style="?android:attr/borderlessButtonStyle"
                android:background="@drawable/button_back_white"
                android:text="@string/ok"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold" />


            <Button
                android:id="@+id/dialog_language_cancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                style="?android:attr/borderlessButtonStyle"
                android:background="@drawable/button_back_white"
                android:text="@string/cancel"
                android:textAllCaps="false"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

实际设计如图:

还有Title布局。。。名字是dialog_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/layout_dialog_back"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/layout_dialog_back">

        <TextView
            android:id="@+id/txt_dialog_picktime_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/txt_complete_the_info"
            android:textAllCaps="false"
            android:textStyle="bold"
            android:textColor="@color/font_gray"
            android:textSize="18sp" />
    </RelativeLayout>
</LinearLayout>

最后是逻辑代码。。。

private void showLanguageDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);//简单的对话框builder
        //以下设置自定义的布局
        LayoutInflater inflater = getLayoutInflater();
        final View layout = inflater.inflate(R.layout.dialog_language_layout, null);
        View titleView = inflater.inflate(R.layout.dialog_title, null);
        builder.setView(layout);
        builder.setCustomTitle(titleView);//设置自定义的标题
         //初始化两个选择的控件
        final RadioButton radioButton_eng = layout.findViewById(R.id.radio_eng);
        final RadioButton radioButton_sim_chn = layout.findViewById(R.id.radio_sim_chn);
        //以下设置中英选项二选一
        radioButton_eng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioButton_eng.isChecked()) {
                    radioButton_sim_chn.setChecked(false);
                }
            }
        });

        radioButton_sim_chn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioButton_sim_chn.isChecked()) {
                    radioButton_eng.setChecked(false);
                }
            }
        });
        //初始化确定、取消两个按钮
        Button btn_ok = layout.findViewById(R.id.dialog_language_ok);
        Button btn_cancel = layout.findViewById(R.id.dialog_language_cancel);
         //对话框标题
        final TextView txt_title = layout.findViewById(R.id.txt_dialog_language_title);
        txt_title.setText("Language/语言");

        final AlertDialog dlg = builder.create();//简单的对话框
        dlg.setCanceledOnTouchOutside(false);//设置点击其他区域时不能取消对话框
        
        //设置确定按钮事务
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //开始更改语言
                Resources resources = getResources();
                Configuration config = resources.getConfiguration();
                DisplayMetrics dm = resources.getDisplayMetrics();
                if (radioButton_eng.isChecked()) {
                    config.locale = Locale.ENGLISH;
                    radioButton_sim_chn.setChecked(false);
                } else if (radioButton_sim_chn.isChecked()) {
                    config.locale = Locale.SIMPLIFIED_CHINESE;
                    radioButton_eng.setChecked(false);
                }
                resources.updateConfiguration(config, dm);//更新语言配置
                if (radioButton_eng.isChecked() || radioButton_sim_chn.isChecked()) {
                    Intent intent = new Intent();
                    intent.setClassName(getPackageName(), "trial.asignment.graduation.ad.AccountSettingActivity");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);//更改语言配置需要重新启动Activity。。。
                }
            }
        });
        //点击取消按钮的事务:啥都不干
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });
        dlg.show();//最后别忘了把我们的Dialog显示出来。。。
 }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值