Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 根据语言打开活动_Android_Xml_Class_Android Layout_Android Intent - Fatal编程技术网

Android 根据语言打开活动

Android 根据语言打开活动,android,xml,class,android-layout,android-intent,Android,Xml,Class,Android Layout,Android Intent,我有一个支持不同语言的应用程序。但是,我有一个布局文件,无论使用何种语言,我都要使用它。 此布局有两个按钮可打开不同的活动,但必须按字母顺序排列。 i、 e.汽车(发动机)-自动(de) 英语版按钮3将打开汽车活动,但德语版按钮1将打开汽车活动。我如何确保这种情况发生,或者我必须根据语言创建不同的活动? 布局文件: <RelativeLayout android:layout_width="match_parent" android:layout_heigh

我有一个支持不同语言的应用程序。但是,我有一个布局文件,无论使用何种语言,我都要使用它。
此布局有两个按钮可打开不同的活动,但必须按字母顺序排列。
i、 e.汽车(发动机)-自动(de) 英语版按钮3将打开汽车活动,但德语版按钮1将打开汽车活动。我如何确保这种情况发生,或者我必须根据语言创建不同的活动?
布局文件:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:orientation="horizontal" >


    <Button 
        android:id="@+id/btnButton1"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="25dp"
        android:text="1" />

    <Button 
        android:id="@+id/btnButton2"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_toRightOf="@+id/btnButton1"
        android:layout_alignBottom="@+id/btnButton1"
        android:layout_marginLeft="5dp"
        android:text="2"/>

    <Button 
        android:id="@+id/btnButton3"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_toRightOf="@+id/btnButton2"
        android:layout_alignBottom="@+id/btnButton2"
        android:layout_marginLeft="5dp"
        android:text="3"/>
</RelativeLayout>

最好看看这是否可行,否则可能会更快更容易地为每种语言创建不同的apk。您可以使用相同的活动类,并为每种语言创建不同的布局或片段XML。请参见上的“设计灵活布局”一节。正如它所说的,确保所有这些布局文件都保持更新将是额外的工作。如果您计划支持多种语言,可以考虑以编程方式添加按钮。然后只需更新每种语言的字符串。请参阅。

您需要为所有按钮提供相同的OnClickListener,并在该listener中检查按钮的文本,如:

OnCLickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                Intent intent = new Intent();

                if (v.getText().equals(getString(R.string.car)) {
                    intent.setClass(Menu.this, Car.class);
                }
                else if (v.getText().equals(getString(R.string.whatever)) {
                    intent.setClass(Menu.this, Whatever.class);
                }
                // ...

                startActivity(intent);
        }
    });

button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);

谢谢-但我已经读过了,但是在文档中看不到任何地方说明或指向我使用1个布局文件的方向,并且取决于语言将取决于哪个按钮做什么,除非我在该链接中遗漏了什么?我建议您使用多个布局文件,例如,
res/layout de/main.xml
res/layout en/main.xml等。该活动完全不知道按钮在屏幕上的位置,因此同一活动可用于所有版面。将根据设备上的区域设置自动选择正确的布局。实际上,我可能会通过编程方式选择添加按钮,因为我的所有字符串都是doneThank you-我使用if语句,但使用它确定语言,而不是使用同一侦听器设置它们
OnCLickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                Intent intent = new Intent();

                if (v.getText().equals(getString(R.string.car)) {
                    intent.setClass(Menu.this, Car.class);
                }
                else if (v.getText().equals(getString(R.string.whatever)) {
                    intent.setClass(Menu.this, Whatever.class);
                }
                // ...

                startActivity(intent);
        }
    });

button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);