我有一个登录屏幕,并添加了一个带有硬编码值的微调器。基于从微调器值中选择语言,例如,如果我选择法语,则活动应更改为法语,我尝试了下面给出的代码:
package com.example.mahindras;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.*;
import java.util.ArrayList;
import java.util.Locale;
public class MahindraActivity extends Activity {
EditText userid,pwd1;
Button loginbut;
Spinner langselect;
TextView userid1;
ResultSet rs1;
String url="jdbc:microsoft:sqlserver://10.0.2.2:1433;DatabaseName=swingmail";
String user="sa";
String pwd="sa";
static ArrayList<String> lang_spinner = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mahindra);
userid1=(TextView)findViewById(R.id.textView1);
userid=(EditText) findViewById(R.id.uname);
pwd1=(EditText) findViewById(R.id.password);
loginbut=(Button) findViewById(R.id.login);
langselect=(Spinner)findViewById(R.id.language);
langselect.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String lang1=(String) arg0.getItemAtPosition(arg2);
if(lang1.equals("français")){
String language="fr";
Locale locale=new Locale(language);
Locale.setDefault(locale);
Resources res=getBaseContext().getResources();
DisplayMetrics dm=res.getDisplayMetrics();
android.content.res.Configuration config=res.getConfiguration();
config.locale=locale;
res.updateConfiguration(config, dm);
Toast.makeText(MahindraActivity.this, "french", 10).show();
refresh();
}
if(lang1.equals("Chinese")){
Locale locale=new Locale("zh");
Locale.setDefault(locale);
Configuration config=new Configuration();
config.locale=locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
private void refresh() {
// TODO Auto-generated method stub
finish();
Intent myIntent = new Intent(MahindraActivity.this, MahindraActivity.class);
MahindraActivity.this.startActivity(myIntent);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
loginbut.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String userid1=userid.getText().toString();
String pass1=pwd1.getText().toString();
Toast.makeText(MahindraActivity.this, "Please Wait"+userid1, 10).show();
login(userid1,pass1);
}
private void login(String userid1, String pass1) {
// TODO Auto-generated method stub
String userrole1 = "";
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con=DriverManager.getConnection(url, user, pwd);
Statement st=con.createStatement();
String str="select userrole from usermaster where m_loginid='"+userid1+"'and m_password='"+pass1+"'" ;
String str1="select lang from language";
ResultSet rs= st.executeQuery(str);
rs1=st.executeQuery(str1);
while(rs.next()){
userrole1=rs.getString(1);
}
if(userrole1.equals("admin")){
Intent intent=new Intent(MahindraActivity.this,MainActivity.class);
startActivity(intent);
}
/*while(rs1.next()){
String langs=rs1.getString(1);
ArrayAdapter langadpt=new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, lang_spinner.add(langs));
langselect.setAdapter(langadpt);
}
;*/
}
catch(Exception e){
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_mahindra, menu);
return true;
}
}
但是活动不会随着语言环境而改变,我的代码是否正确或者我应该在这个程序中做些什么改变。
我已经刷新了 ItemSelectedListner 中的 Activity
langselect.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String lang1=(String) arg0.getItemAtPosition(arg2);
if(lang1.equals("français")){
String language="fr";
Locale locale=new Locale(language);
Locale.setDefault(locale);
Resources res=getBaseContext().getResources();
DisplayMetrics dm=res.getDisplayMetrics();
android.content.res.Configuration config=res.getConfiguration();
config.locale=locale;
res.updateConfiguration(config, dm);
Toast.makeText(MahindraActivity.this, "french", 10).show();
refresh();
}