问题描述
下面是我的工作登录代码。 我现在希望在个人资料页面上显示登录人的“姓名”。 找遍了,还是没找到。
例如:使用名称“example@mail.com”(下面代码中的“用户名”)登录的人员。
我希望“example@mail.com”显示在不同页面的TextView
上。
谢谢您的帮助!
private EditText editTextUserName;
private EditText editTextPassword;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUserName = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
}
public void invokeLogin(View view){
username = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
login(username,password);
}
private void login(final String username, String password) {
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://calisapp.esy.es/login.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
String s = result.trim();
loadingDialog.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(MainActivity.this, UserProfile.class);
intent.putExtra(USER_NAME, username);
finish();
startActivity(intent);
} else {
editTextUserName.requestFocus();
editTextUserName.setError("Wrong E-mail address or password");
}
}
}
LoginAsync la = new LoginAsync();
la.execute(username, password);
}
1楼
有三种方法,你可以这样做
1. 使用
2. 将值存储在
3. 通过传递值。
2楼
如果他们的登录成功,请使用 SharedPreferences 来存储它。 像这样的东西:
SharedPreferences myPrefs = getSharedPreferences(PREF_KEY, Context.MODE_PRIVATE);
myPrefs.edit().putString(EMAIL_KEY, someEmail@example.com).apply();
myPrefs.edit().putString(PASSWORD_KEY, somePassword).apply();
3楼
编辑:由于某种原因不起作用。
谢谢你们。 我不知道你可以使用多个意图,所以我这样做了。 如果有人碰巧需要知道我是如何做到的。 使用共享首选项可能更好,但我现在只需要在多一页上使用它,而且我不喜欢让事情变得更难。
我在成功登录时向登录类添加了第二个意图,它将用户名字符串传送到我的设置/配置文件。
Intent intent2 = new Intent (MainActivity.this, Settings.class);
intent2.putExtra(USER_NAME, username);
在设置页面上,我通过一个 intent.getStringExtra 调用它。 我使用了一个文本视图来显示字符串。
Intent intent2 = getIntent();
String username = intent2.getStringExtra(MainActivity.USER_NAME);
TextView showEmail = (TextView) findViewById(R.id.SettingsShowEmail);
showEmail.setText(username);
4楼
Intent intent2 = new Intent (MainActivity.this, Settings.class);
intent2.putExtra("username", username);
startActivity(intent2);
关于设置活动
String username = getIntent().getExtras().getString("username");
TextView showEmail = (TextView) findViewById(R.id.SettingsShowEmail);
showEmail.setText(username);