当前位置: 首页 > 知识库问答 >
问题:

在android studio中更改语言表单用户选择

穆宾白
2023-03-14

如果用户选择任何语言的应用程序更改,language.but我的应用程序内容是从json检索数据api.how改变它?

在此处输入图像描述

    package com.blog.navdrawer;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.blog.navdrawer.JSONParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

import static android.content.Context.MODE_PRIVATE;
import static com.blog.navdrawer.R.string.language;

/**
 * Created by ewall-07 on 3/12/16.
 */
public class DemoFragone extends Fragment {



    private JSONParser jsonparser = new JSONParser();
    private TextView tv,tv1;

    String a,b,c;
    private JSONObject jsonobject = null;

    String languagesave;
    private Locale myLocale;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // View v = inflater.inflate(R.layout.fragment_fragone,container,false);


        View v = inflater.inflate(R.layout.fragment_fragone,container,false);
        tv = (TextView) v.findViewById(R.id.text_tv);
        tv1 = (TextView) v.findViewById(R.id.text_tv1);
        new retrievedata().execute();

       language();
/*

        //it help us to change the font style...
        Typeface type = Typeface.createFromAsset(getActivity().getAssets(),"DroidSerif-Regular.ttf");
        TextView tv1 = (TextView) v.findViewById(R.id.text_tv1);
        tv1.setTypeface(type);*/
        return v;




    }

    private void language() {

        String langPref = "Language";
        SharedPreferences sharedpreferences = this.getActivity().getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(getString(R.string.language),languagesave);
        editor.commit();
    }





    class retrievedata extends AsyncTask<String, String, String> {



        @Override
        protected String doInBackground(String... strings) {




            if(languagesave == "en") {

                jsonobject = jsonparser.makeHttpRequest("http://www.json-generator.com/api/json/get/cegNjwgBTS?indent=2");

                Log.d("app data", jsonobject.toString());

                try {

                    a = jsonobject.getString("name");
                    b = jsonobject.getString("data");
                    c = jsonobject.getString("image_url");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            else if(languagesave == "ar"){
                    jsonobject = jsonparser.makeHttpRequest("http://www.json-generator.com/api/json/get/cegNjwgBTS?indent=2");

                    Log.d("app data", jsonobject.toString());

                    try {

                        a = jsonobject.getString("name_ar");
                        b = jsonobject.getString("data_ar");
                        c = jsonobject.getString("image_url");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            return a;
        }
        protected  void onPostExecute(String a){
            tv.setText(a);
            tv1.setText(b);

            ImageView image = (ImageView) getActivity().findViewById(R.id.image_one);
            Picasso.with(getActivity()).load(c).error(R.drawable.error).placeholder(R.drawable.progress_aniamtion).noFade().into(image);
        }
        }

@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());
    }

} }

MainActivity :

package com.blog.navdrawer.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.blog.navdrawer.R;
import com.blog.navdrawer.adapter.CountryLanguageAdapter;

public class CountryLanguageActivity extends AppCompatActivity implements OnClickListener {

    private Button mSaveButton;
    private ListView mCountryList, mLanguageList;
    private ArrayAdapter<String> mLanguageAdapter, mCountryAdaptor;
    String languagesave;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addActionbar();
        setContentView(R.layout.activity_country_language);
        initialization();
    }

    private void addActionbar() {
        //noinspection ConstantConditions
        getSupportActionBar().setCustomView(R.layout.actionbar);
        TextView TvTitle = (TextView) findViewById(getResources()
                .getIdentifier("action_bar_title", "id", getPackageName()));
    }

    private void initialization() {

       //ActionBar bar = getActionBar();
       // bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f79f04")));
       //bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
       //bar.setCustomView(R.layout.actionbar);

        mCountryList = (ListView) findViewById(R.id.list_country);
        mLanguageList = (ListView) findViewById(R.id.list_language);
        mSaveButton = (Button) findViewById(R.id.button_save_continue);

        String[] country = { "Saudi Arabia - Riyadh", "Kuwait", "Qatar", "Bahrain" };
        String[] language   = { "English", "Arabic"};
        int[] countryIcon = { R.drawable.ic_saudi, R.drawable.ic_kuwait , R.drawable.ic_qatar, R.drawable.ic_bahrain};
        int[] languageIcon = { R.drawable.ic_english, R.drawable.ic_arabic };
        int countryList = 1;
        int languageList = 2;
        String[] languagesave = language;

        //Create a customer adapter for the language list
        mLanguageAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, country);
        mCountryList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mCountryList.setAdapter(new CountryLanguageAdapter(CountryLanguageActivity.this, country, countryIcon, countryList));
        //Create a customer adapter for the country list
        mCountryAdaptor = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, language);
        mLanguageList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mLanguageList.setAdapter(new CountryLanguageAdapter(CountryLanguageActivity.this, language, languageIcon, languageList));

        ViewTreeObserver listVTO = mCountryList.getViewTreeObserver();
        listVTO.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mCountryList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                resizeListView(mCountryList);
            }
        });

        ViewTreeObserver listVTO1 = mLanguageList.getViewTreeObserver();
        listVTO1.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLanguageList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                resizeListView(mLanguageList);
            }
        });

        mSaveButton.setOnClickListener(this);

        SharedPreferences sharedpreferences = CountryLanguageActivity.this.getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(getString(R.string.language),languagesave);
        editor.commit();

    }

    //onclick even for the save and continue button
    public void onClick(View v ) {
        //ViewDialog alert = new ViewDialog();
        //alert.showDialog(ListViewMultipleSelectionActivity.this, "Please select the country");
        Intent intent = new Intent(CountryLanguageActivity.this,LeftRightNavigationActivity.class);


        SharedPreferences sharedpreferences = CountryLanguageActivity.this.getSharedPreferences(getString(R.string.file_Save),MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(getString(R.string.language),languagesave);
        editor.commit();


        startActivity(intent);
    }

    // Dynamically set the height for the items in list view
    private void resizeListView(ListView listView) {
        ListAdapter adapter = listView.getAdapter();
        int count = adapter.getCount();
        int itemsHeight = 0;
        // Your views have the same layout, so all of them have
        // the same height
        View oneChild = listView.getChildAt(0);
        if( oneChild == null)
            return;
        itemsHeight = oneChild.getHeight();
        // Resize your list view
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)listView.getLayoutParams();
        params.height = itemsHeight * count;
        listView.setLayoutParams(params);
    }
}

在此处输入图像描述

共有1个答案

谢叶五
2023-03-14

查看我的代码:

    Private void changeLanguage (final int position) {
    final SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();
    if (position == 1) {
        final Locale myLocale = new Locale("ar");
        final Resources res = getResources();
        final DisplayMetrics dm = res.getDisplayMetrics();
        final Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        editor.putString("KEY","ar");
    } else {
        final Locale myLocale = new Locale("en");
        final Resources res = getResources();
        final DisplayMetrics dm = res.getDisplayMetrics();
        final Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        editor.putString("KEY","en");
    }
    editor.apply();
}

在现场活动中输入以下代码

final SharedPreferences sharedPreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
        languagesave = sharedPreferences.getString("KEY", "en");

我已经更新了帖子。

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

                changeLanguage(position);

                final Intent refresh = new Intent(MainActivity.this, MainActivity.class);
                startActivity(refresh);
                finish();

            }
        });
 类似资料:
  • 我希望我的应用程序支持三种语言西班牙语,葡萄牙语 1)3个可绘制文件夹可绘制-es,可绘制-pt,可绘制。 2)3个值文件夹值-es,值-pt,值。根据语言更改String.xml值。 我有imageView来选择语言。当点击它时,菜单打开,包括选项英语、西班牙语、葡萄牙语。 我通过以下代码在选项选择上设置应用程序内部的区域设置 我已在清单中声明-android:configChanges=“lo

  • 问题内容: 我有一个需要更改的地方(添加2列),然后更新同一张表。 这是我尝试过的查询: 我需要一次运行上述两个查询。 我正在使用Talend ETL工具,在这里我们有一个组件tMssqlrow,它允许我们运行多个查询(我在单个组件中使用10到15个更新查询)。 但是上面的查询不起作用。 我在数据库Microsoft SQL中进行了测试。我收到以下错误: 消息207,第16级,州1,第5行 无效的

  • 问题内容: 现在我知道苹果不推荐这样做。 通常,您不应在应用程序内更改iOS系统语言(通过使用AppleLanguages首选项键)。这违反了在设置应用程序中切换语言的基本iOS用户模型,并且还使用了未记录的首选项键,这意味着将来某个时候,键名可能会更改,这会破坏您的应用程序。 但是,这是一个可以随时更改语言的应用程序,请相信我。我也知道有人问过这个问题:在运行iOS时以编程方式实时更改语言。但是

  • 在我的应用程序中,用户应该能够切换区域设置(用于在页面上呈现文本的语言)。大量教程使用FacesContext.get货币实例()。getViewRoot()。setLocale()。例如:http://www.mkyong.com/jsf2/jsf-2-internationalization-example/.但是,这在JSF 2.0中根本不起作用(它在1.2中确实起作用)。这种语言从不切换。

  • UMD™选单语言     选择UMD™Video选单的显示语言。

  • 问题内容: 我正在编写一个简单的脚本,该脚本可以重新启动hadoop从属服务器。在脚本中,我必须以root用户身份进行一些初始更改。之后,我必须更改为用户“ hadoop”并执行命令集。我使用os.system运行命令,但是我怀疑它是否运行良好。例如: 再一次,我必须以root用户身份执行一些命令,然后再次成为用户“ hadoop”并执行: 我在这里有三个问题, os.system是我可以用来发出