-1

主なアクティビティでは、ユーザーの言語セットを表示します。Androidプログラミングでユーザーが選択した言語(またはロケール)はどのように決定しますか?

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textViewSelection" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" 
    android:gravity="center_horizontal"> 

    <RadioButton 
     android:id="@+id/english_lang" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="onRadioButtonClicked" 
     android:text="@string/langtext_english" 
     android:textSize="22dp" 
     android:padding="10dp" /> 

    <RadioButton 
     android:id="@+id/indonesian_lang" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:onClick="onRadioButtonClicked" 
     android:text="@string/langtext_indonesian" 
     android:textSize="22dp" 
     android:padding="10dp" /> 
</RadioGroup> 

ユーザーが言語を選択すると、私はMainActivityFragment.javaクラス内のresディレクトリの下に

public void onRadioButtonClicked(View view){ 
     boolean checked = ((RadioButton) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.english_lang: 
       if (checked) 
        // 
        break; 
      case R.id.french_lang: 
       if (checked) 
        // Ninjas rule 
        break; 
     } 

    } 

に位置onRadioButtonClicked()メソッドにジャンプし、私は値-FRと呼ばれる新しいフォルダを作成していると私は置かfrench値を持つstring.xml

1)ユーザーの選択した言語をアプリコンテキスト全体に渡す必要がありますか?

2)私の方法は使用されないのはなぜですか?

あなたはコードの下に使用してアプリの言語を変更することができます

package com.a2ndnumber.a2ndnumber; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.RadioButton; 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public class MainActivityFragment extends Fragment { 

    private ArrayAdapter<String> countryList_Adapter; 

    public MainActivityFragment() { 
    } 

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

     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     return rootView; 
    } 


    public void onRadioButtonClicked(View view){ 
     boolean checked = ((RadioButton) view).isChecked(); 

     // Check which radio button was clicked 
     switch(view.getId()) { 
      case R.id.english_lang: 
       if (checked) 
        // 
        break; 
      case R.id.indonesian_lang: 
       if (checked) 
        // Ninjas rule 
        break; 
     } 

    } 

    /* 
    * When user is selecting the country, in background we grab 25 numbers of each country, 
    * so that we are ready in next fragment where we let them choose the number 
    * */ 
    public class FetchNumbers_Task extends AsyncTask<Void, Void, Void>{ 

     private final String LOG_TAG = FetchNumbers_Task.class.getSimpleName(); 

     /** 
     * Override this method to perform a computation on a background thread. The 
     * specified parameters are the parameters passed to {@link #execute} 
     * by the caller of this task. 
     * <p/> 
     * This method can call {@link #publishProgress} to publish updates 
     * on the UI thread. 
     * 
     * @param params The parameters of the task. 
     * @return A result, defined by the subclass of this task. 
     * @see #onPreExecute() 
     * @see #onPostExecute 
     * @see #publishProgress 
     */ 
     @Override 
     protected Void doInBackground(Void... params) { 
      return null; 
     } 
    } 
} 

答えて

1

MainActivityFragment.java:

public void setLocale(String lang) { 
    myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, Youractivity.class); // refresh the activity 
    refresh.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    refresh.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(refresh); 
    finish(); 
    } 

とラジオボタンのクリックリスナーを取得するには、使用:

mRadioHindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (b){ 
       Log.d("SettingsActivity::","onCheckedChanged hindi" + b); 
       PreferenceManager.saveLanguage("hi"); 
       setLocale("hi"); 
      } 
     } 
    }); 

すべてのラジオボタンにsetOnCheckedChangeListenerを使用します。

共有環境設定で言語を保存し、次にユーザーがアプリを開いたときにsharedPreferenceから言語を取得してsetLocale()メソッドを呼び出します。

+0

お返事ありがとうございます。これらのコードはどこに行きますか? MainActivity.javaまたは??? – user3705478

+0

言語を選択するオプションがあるアクティビティ。 –

+0

私は活動とその断片を持っています。だから、どこに行くべきか、そしてどの方法の下で? – user3705478

関連する問題