我是使用Android studio的初学者,这是我的第一个项目。
我在主要活动中使用了一个带有5种语言的微调器,只在第一个活动中语言正在改变,但当我点击按钮将我切换到下一页仍然作为默认语言时,下一个活动不会改变,我希望代码继续我的项目?
此外,我还将语言字符串和值文件夹设置为本地帮助程序类。
我想要的代码可以帮助我继续翻译下一个活动。
布局代码
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/language_background"
tools:context="com.example.dell.egyptholidayapp.Activities.LanguageActivity">
<TextView
android:id="@+id/language_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="11dp"
android:text="SELECT YOUR LANGUAGE"
android:textColor="@color/colorWhite"
android:textSize="20dp"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/logo_image"
android:layout_width="120sp"
android:layout_height="120sp"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="43dp"
android:adjustViewBounds="false"
android:contentDescription=""
android:src="@mipmap/app_icon2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/language_title" />
<android.support.v7.widget.CardView
android:id="@+id/selecting_language"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="@+id/logo_image"
android:layout_centerHorizontal="true"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="92dp"
app:cardBackgroundColor="@color/colorWhite"
app:cardCornerRadius="15dp"
app:cardElevation="10dp"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/logo_image">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner"
android:layout_width="284dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<Button
android:id="@+id/next_button"
android:layout_width="140dp"
android:layout_height="40dp"
android:layout_below="@+id/selecting_language"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@color/colorBlue2"
android:text="NEXT"
android:textColor="@color/colorWhite"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/selecting_language" />
</android.support.constraint.ConstraintLayout>
java代码
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.dell.egyptholidayapp.R;
import java.lang.reflect.Field;
public class LanguageActivity extends Activity implements AdapterView.OnItemSelectedListener {
private TextView myLanguageTittle;
private Button myNextButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
try{
Field popup = Spinner.class.getDeclaredField("mPopup");
popup.setAccessible(true);
android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow)popup.get(spinner);
popupWindow.setHeight(500);
}
catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e){
}
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.language_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "AvenirNextLTPro-Regular.otf");
myLanguageTittle = (TextView) findViewById(R.id.language_title);
myNextButton = (Button) findViewById(R.id.next_button);
myLanguageTittle.setTypeface(myTypeface);
myNextButton.setTypeface(myTypeface);
myNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(LanguageActivity.this,LoginActivity.class);
startActivity(i);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sSelected = parent.getItemAtPosition(position).toString();
Toast.makeText(this, sSelected, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
}
答案 0 :(得分:1)
这是因为当您更改First活动的语言时。您已将其编程为已更改,而您的其他活动未发现您在第一个活动中更改语言的事实。
答案是 使用
共享偏好
因此,每当您更改共享首选项中的语言存储时,以便在转到下一个活动时,您可以获得公共共享首选项并相应地更改语言。
答案 1 :(得分:0)
由于频繁的API更改,在运行时更改语言非常混乱。幸运的是,有关如何在所有Android API级别上执行此操作的非常好的指南。 试一试https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758