如何做语言的隐藏跟切换

来源:互联网 发布:手机视频交友软件 编辑:程序博客网 时间:2024/05/15 13:10

首先看看设置语言的路径:

  Android系统Setting程序中对于语言设置这块的内容。具体位置有以下两处:
        1)、设置显示语言:Settings -> Language & keyboard -> Select language 

      
    Settings工程中,Settings -> Language & keyboard界面所对应的Java代码和Preference布局如下:

      

<android_root>/packages/apps/Settings/src/com/android/settings/LanguageSettings.java

  1.         <android_root>/packages/apps/Settings/res/xml/language_settings.xml

 

1、Settings -> Language & keyboard -> Select language
    在<android_root>/packages/apps/Settings/res/xml/language_settings.xml中,该模块的Preference布局为:

    <PreferenceScreen
            android:fragment="com.android.settings.LocalePicker"
            android:key="phone_language"
            android:title="@string/phone_language"/>

    <com.android.settings.inputmethod.SpellCheckersPreference
            android:key="spellcheckers_settings"
            android:title="@string/spellcheckers_settings_title"/>

 

LocalePicker Activity将取得的locale字符串进行了一些处理,然后创建了ArrayAdapter<Loc> adapter,并绑定到ListActivity的ListView上。当用户点击ListView上的Item时,再将选中的locale信息设置到Android系统中。

 

文件frameworks\base\core\java\com\android\internal\app\LocalePicker.java

public static ArrayAdapter<LocaleInfo> constructAdapter(Context context,
            final int layoutId, final int fieldId) {
        final Resources resources = context.getResources();
        final String[] locales = Resources.getSystem().getAssets().getLocales();
        final String[] specialLocaleCodes = resources.getStringArray(R.array.special_locale_codes);
        final String[] specialLocaleNames = resources.getStringArray(R.array.special_locale_names);

              Arrays.sort(locales);
              final int origSize = locales.length;
              final LocaleInfo[] preprocess = new LocaleInfo[origSize];
              int finalSize = 0;
              for (int i = 0 ; i < origSize; i++ ) {
              final String s = locales[i];
              final int len = s.length();
              if (len == 5) {
                String language = s.substring(0, 2);
                String country = s.substring(3, 5);
                final Locale l = new Locale(language, country);

              //处理切换语句

                /*

                 ***************************

                */

                if (language.equals("zh") && country.equals("CN")||language.equals("zh") && country.equals("TW"))
                  continue;
                              if (finalSize == 0) {
                    if (DEBUG) {
                        Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l)));
                    }
                    preprocess[finalSize++] =
                            new LocaleInfo(toTitleCase(l.getDisplayLanguage(l)), l);

 

 } else {
                    // check previous entry:
                    //  same lang and a country -> upgrade to full name and
                    //    insert ours with full name
                    //  diff lang -> insert ours with lang-only name
                    if (preprocess[finalSize-1].locale.getLanguage().equals(
                            language)) {
                        if (DEBUG) {
                            Log.v(TAG, "backing up and fixing "+
                                    preprocess[finalSize-1].label+" to "+
                                    getDisplayName(preprocess[finalSize-1].locale,
                                            specialLocaleCodes, specialLocaleNames));
                        }
                        preprocess[finalSize-1].label = toTitleCase(
                                getDisplayName(preprocess[finalSize-1].locale,
                                        specialLocaleCodes, specialLocaleNames));
                        if (DEBUG) {
                            Log.v(TAG, "  and adding "+ toTitleCase(
                                    getDisplayName(l, specialLocaleCodes, specialLocaleNames)));
                        }
                        preprocess[finalSize++] =
                                new LocaleInfo(toTitleCase(
                                        getDisplayName(
                                                l, specialLocaleCodes, specialLocaleNames)), l);
                    } else {
                        String displayName;
                        if (s.equals("zz_ZZ")) {
                            displayName = "Pseudo...";
                        } else {
                            displayName = toTitleCase(l.getDisplayLanguage(l));
                        }
                        if (DEBUG) {
                            Log.v(TAG, "adding "+displayName);
                        }
                        preprocess[finalSize++] = new LocaleInfo(displayName, l);
                    }
                }
            }
        }

        final LocaleInfo[] localeInfos = new LocaleInfo[finalSize];
        for (int i = 0; i < finalSize; i++) {
            localeInfos[i] = preprocess[i];
        }
        Arrays.sort(localeInfos);

        final LayoutInflater inflater =
                (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return new ArrayAdapter<LocaleInfo>(context, layoutId, fieldId, localeInfos) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view;
                TextView text;
                if (convertView == null) {
                    view = inflater.inflate(layoutId, parent, false);
                    text = (TextView) view.findViewById(fieldId);
                    view.setTag(text);
                } else {
                    view = convertView;
                    text = (TextView) view.getTag();
                }
                LocaleInfo item = getItem(position);
                text.setText(item.toString());
                text.setTextLocale(item.getLocale());

                return view;
            }
        };
    }

在红色字体里面做切换处理便可以实现。

0 0
原创粉丝点击