如何实现多语言android应用程序?

如何解决如何实现多语言android应用程序?

我正在创建一个使用 expendableListView 在 android studio 中实现多语言的应用程序。使用 Youtube 上的一些教程,我实现了多语言,但它不起作用。我不知道为什么,但根据性能,似乎重新创建了应用程序,但它并没有改变语言。

Expendablelistadapter在这里

public class Customexpandablelistadapter extends Baseexpandablelistadapter {


private static final int GROUP_TYPE_0 = 0;
private static final int GROUP_TYPE_1 = 1;

private static final int CHILD_TYPE_1 = 0;
private static final int CHILD_TYPE_2 = 1;


private Context context;
private List<String> listTitulo;
private HashMap<String,Contacto> expandableListDetalles;
Contextwrapper contextwrapper;
public Customexpandablelistadapter(Context context,List<String> listTitulo,HashMap<String,Contacto> expandableListDetalles) {
    this.context = context;
    this.listTitulo = listTitulo;
    this.expandableListDetalles = expandableListDetalles;
    contextwrapper=new Contextwrapper(context);
}


@Override
public View getChildView(final int groupPosition,int childPosition,boolean isLastChild,View convertView,ViewGroup parent) {

    final Contacto contacto = (Contacto) getChild(groupPosition,childPosition);



    LayoutInflater inflater = (LayoutInflater) this.context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Integer childType = new Integer(getChildType(groupPosition,childPosition));


    // We need to create a new "cell container"
    if (convertView == null || convertView.getTag() != childType) {
        switch (childType) {
            case CHILD_TYPE_1:
                convertView = inflater.inflate(R.layout.language_layout,null);
                convertView.setTag(childType);
                break;
            case CHILD_TYPE_2:
                convertView = inflater.inflate(R.layout.language_layout,null);
                convertView.setTag(childType);
                break;
            default:
                // Maybe we should implement a default behavIoUr but it should be ok we kNow there are 4 child types right?
                break;
        }
    }
    // We'll reuse the existing one
    else {
        // There is nothing to do here really we just need to set the content of view which we do in both cases
    }

    switch (childType) {
        case CHILD_TYPE_1:
            RadioGroup radioGroup2=convertView.findViewById(R.id.radioMainGroup);
            RadioButton rButton3= convertView.findViewById(R.id.radioButton1);
            rButton3.setText(contacto.geTradio1());
            rButton3.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if (isChecked){

                    }
                }
            });

            RadioButton rButton4= convertView.findViewById(R.id.radioButton2);
            rButton4.setText(contacto.geTradio2());
            rButton4.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if (isChecked){

                    }
                }
            });


            break;
        case CHILD_TYPE_2:
            RadioGroup radioGroup=convertView.findViewById(R.id.radioMainGroup);
            RadioButton rButton= convertView.findViewById(R.id.radioButton1);
            rButton.setText(contacto.geTradio1());
            rButton.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if (isChecked){
                        setLocale("fa-rIR");
                        ((Activity)context).recreate();

                    }
                }
            });


            RadioButton rButton2 = convertView.findViewById(R.id.radioButton2);
            rButton2.setText(contacto.geTradio2());
            rButton2.setonCheckedchangelistener(new CompoundButton.OnCheckedchangelistener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if (isChecked){
                        setLocale("en");
                        ((Activity)context).recreate();

                    }
                }
            });

            break;
    }





    return convertView;
}

public void setLocale(String s) {
    Locale locale=new Locale(s);
    Locale.setDefault(locale);
    Configuration config=new Configuration();
    config.locale=locale;
    ((Activity)context).getBaseContext().getResources().updateConfiguration(config,((Activity)context).getBaseContext().getResources().getdisplayMetrics());
    SharedPreferences.Editor editor=context.getSharedPreferences("settings",Context.MODE_PRIVATE).edit();
    editor.putString("My_Lang",s);
    editor.apply();
}

public void loadLocale(){
    SharedPreferences prefs=context.getSharedPreferences("settings",Activity.MODE_PRIVATE);
    String language=prefs.getString("My_Lang","");
    setLocale(language);
}


@Override
public View getGroupView(int groupPosition,boolean isExpanded,ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) this.context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    String nombre = (String) getGroup(groupPosition);
    Contacto contacto = (Contacto) getChild(groupPosition,0);

    Integer groupType = new Integer(getGroupType(groupPosition));

    // We need to create a new "cell container"
    if (convertView == null || convertView.getTag() != groupType) {
        switch (groupType) {
            case GROUP_TYPE_0 :
                convertView = inflater.inflate(R.layout.list_group,null);
                break;

            default:
                convertView = inflater.inflate(R.layout.list_group,null);
                // Maybe we should implement a default behavIoUr but it should be ok we kNow there are 3 group types right?
                break;
        }
    }
    // We'll reuse the existing one
    else {
        // There is nothing to do here really we just need to set the content of view which we do in both cases
    }

    switch (groupType) {
        case GROUP_TYPE_0 :
            ImageView imgV=convertView.findViewById(R.id.groupImageView);

            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),contacto.getImg());
            imgV.setimageBitmap(bitmap);

            TextView txtNombre = convertView.findViewById(R.id.txtGroupNombre);
            txtNombre.setText(nombre);
            break;

        default:
            ImageView imgV1=convertView.findViewById(R.id.groupImageView);

            Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(),contacto.getImg());
            imgV1.setimageBitmap(bitmap1);

            TextView txtNombre1 = convertView.findViewById(R.id.txtGroupNombre);
            txtNombre1.setText(nombre);
            break;
    }

    return convertView;

    
}


@Override
public int getGroupType(int groupPosition) {
    switch (groupPosition) {
        case 0:
            return GROUP_TYPE_0;
        default:
            return GROUP_TYPE_1;

    }
}

@Override
public int getChildType(int groupPosition,int childPosition) {
    switch (groupPosition) {
        case 0:
            return CHILD_TYPE_1;
        default:
            return CHILD_TYPE_2;
    }
}


@Override
public int getGroupCount() {
    return this.listTitulo.size();
}

@Override
public int getChildrenCount(int groupPosition) {

    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    return this.listTitulo.get(groupPosition);
}

@Override
public Object getChild(int groupPosition,int childPosition) {
    return this.expandableListDetalles.get(this.listTitulo.get(groupPosition));//////////////////////////////////////////////
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition,int childPosition) {
    return childPosition;
}

@Override
public boolean hasstableIds() {
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
    return true;
}

@Override
public int getChildTypeCount() {
    return 2;
}



@Override
public int getGroupTypeCount() {
    return 2;
}

}

您应该注意到,因为我无法直接访问 Activity,所以我将上下文转换为 (Activity)context 以使用诸如 getBaseContext() 之类的函数,有时我单独使用上下文。

我的 string.xml 是“英语”语言:

<resources>
<string name="app_name">ExpandableListView</string>
<string name="dracula">Dracula</string>
<string name="white">white</string>
<string name="english">English</string>
<string name="persian">فارسی</string>
<string name="lang">Language</string>
<string name="theme">Theme</string>

strings.xml(fa-rIR) 是“波斯”语言:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">ExpandableListView</string>
    <string name="dracula">دراکولا</string>
    <string name="white">سفید</string>
    <string name="english">English</string>
    <string name="persian">فارسی</string>
    <string name="lang">زبان</string>
    <string name="theme">زمینه</string>
</resources>

这里是 mainActivity.class :

public class MainActivity extends AppCompatActivity {

private ExpandableListView expandableListView;
private expandablelistadapter expandablelistadapter;
private List<String> expandableListNombres;
private HashMap<String,Contacto> listaContactos;
private int lastExpandedPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    init();

    expandableListView.setAdapter(expandablelistadapter);

    expandableListView.setonGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            if(lastExpandedPosition != -1 && groupPosition != lastExpandedPosition){
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });



}

private void init() {
    this.expandableListView = findViewById(R.id.expandableListView);
    this.listaContactos = getContactos();
    this.expandableListNombres = new ArrayList<>(listaContactos.keySet());
    this.expandablelistadapter = new Customexpandablelistadapter(this,expandableListNombres,listaContactos);

}


private HashMap<String,Contacto> getContactos() {
    HashMap<String,Contacto> listaC = new HashMap<>();

    listaC.put(getString(R.string.lang),new Contacto(R.string.english,R.string.persian,R.drawable.img_11));
    listaC.put(getString(R.string.theme),new Contacto(R.string.dracula,R.string.white,R.drawable.img_22));

    return listaC;
}

}

还有,这是 Contacto.java:

package elv.iso.com.expandablelistview;

公开课联系{

private int radio1,radio2;
private int img;


public Contacto(int radio1,int radio2,int img1) {
    this.radio1 = radio1;
    this.radio2 = radio2;
    this.img=img1;

}


public int geTradio1() {
    return radio1;
}

public int geTradio2() {
    return radio2;
}

public int getImg() {
    return img;
}

}

如您所见,这是一个简单的应用程序。但我不知道为什么这不起作用。 也许应用程序的图形视图很有用: app view

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?