-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> 

사용자가 언어를 선택하면, 내가 onRadioButtonClicked()

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; 
     } 

    } 

에서 고해상도 디렉토리, 내가 만든 새 폴더라는 값-FR 내가 배치 MainActivityFragment.java 클래스 내부에있는 방법으로 이동 프랑스어 값을 갖는 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