2

我正在开发一个我正在使用片段的 android 应用程序。并且在单击列表项后,我将语言从英语更改为阿拉伯语,并且工作正常。

但是我希望即使我们从设备强制关闭应用程序后,所选语言仍保留在整个应用程序中......但这不会发生在我身上......

如果我与 Activity 一起使用,那么我有电话

setContentView(R.layout.main); 

在 onResume() 方法和它的工作正常。

但我的问题是,我们如何设置带有片段的视图或在 onResume() 方法上调用 onCreateView 方法,因为 onCreateView() 方法返回自己的视图。所以尝试了很多来解决它,但我不能。所以请有什么办法解决吗??

提前致谢。

这是我的代码:

package com.example.okazz.Settings;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.okazz.R;

public class Languages extends Fragment {
// private int current = -1;
View view;
Context ct;
SharedPreferences sp1, sp;
Boolean Ischeck = false;
String User_id, countryid;
Button settings;
String[] arrLanguages = new String[] { "English", "Arabic" };
String req;
ListView lv;
private Locale myLocale;

public Languages(Context c) {

    this.ct = c;
}

/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view = inflater.inflate(R.layout.languagechange, container, false);

    SharedPreferences sp = getActivity().getSharedPreferences("key", 0);
    User_id = sp.getString("userid=", User_id);
    countryid = sp.getString("countryid", countryid);
    settings = (Button) view.findViewById(R.id.btnsett);
    lv = (ListView) view.findViewById(android.R.id.list);
    lv.setAdapter(new MobileArrayAdapter(ct, arrLanguages));
    settings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager = getFragmentManager();
            manager.popBackStack();
            Log.d("Back to Home", "back button pressed");
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // get selected items
            String selectedValue = (String) lv.getAdapter().getItem(
                    position);
            Toast.makeText(ct, selectedValue, Toast.LENGTH_SHORT).show();
            // setResult(RESULT_OK, new Intent().putExtra("language",
            // arrLanguages[position]));
            // Configuration config = new Configuration();
            String lang = null;
            switch (position) {
            case 0:
                lang = "en";
                break;
            case 1:
                lang = "ar";
                break;

            default:

                break;
            }

            changeLang(lang);

            // SharedPreferences sp = getActivity().getSharedPreferences(
            // "check", 0);
            //
            // boolean checked = sp.getBoolean("Ischeck", false);
            //
            // if (checked == true) {
            lv.setItemChecked(position, true);
            // }

        }

    });
    return view;
}

public void loadLocale() {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    String language = prefs.getString(langPref, "");
    changeLang(language);

}

public void saveLocale(String lang) {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(langPref, lang);
    editor.commit();
}

public void changeLang(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;
    myLocale = new Locale(lang);
    Log.v("My Language", myLocale + "");
    saveLocale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    ct.getResources().updateConfiguration(config,
            ct.getResources().getDisplayMetrics());

}

class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_changelanguage, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_changelanguage,
                parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView
                .findViewById(R.id.imageView1);
        textView.setText(values[position]);

        String s = values[position];

        System.out.println("**** Position ****" + s);
        if (lv.isItemChecked(position)) {
            imageView.setVisibility(View.VISIBLE);
            Ischeck = true;
            SharedPreferences sp = getActivity().getSharedPreferences(
                    "check", 0);
            SharedPreferences.Editor sedt = sp.edit();
            sedt.putBoolean("Ischeck", Ischeck);
            sedt.clear();
            sedt.commit();
        } else {
            imageView.setVisibility(View.GONE);

        }

        return rowView;
    }
}

@Override
public void onConfigurationChanged(
        android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (myLocale != null) {
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getActivity().getResources().updateConfiguration(newConfig,
                getActivity().getResources().getDisplayMetrics());
    }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    loadLocale();
    super.onResume();
}}
4

2 回答 2

0

您的问题有点难以阅读,但我想我明白了:您想随着语言的变化重新布局片段。

我想最好的片段可能是删除片段,然后将新实例重新添加到您的活动中。

另一种选择是使用活动及其内部片段的 onConfigurationChange 方法:更改语言环境:强制活动重新加载资源?.

您可以通过执行以下操作自己触发此事件:

int lang = //the result of your click inside the list item
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();

switch(lang){
    case ARABIC:
        conf.locale = Locale.WHATEVER;
        break;

    case ENGLISH:
        conf.locale = Locale.ENGLISH;
        break;
}
res.updateConfiguration(conf, dm);
于 2013-08-09T06:06:47.163 回答
0

您可以使用SharedPreferences保存您的语言偏好,并在 onCreate 中使用首选语言重新创建视图

于 2013-08-09T06:11:50.520 回答