java - 登录成功后换屏?(安卓工作室)
问题描述
在我的实习期间,我的任务是接管别人的代码并从事一个项目。我在成功登录后尝试导航到另一个屏幕时遇到了困难。目前,下面的代码将我重定向回原来的主菜单页面一个成功的登录(而一个失败的登录什么都不做)。
我的问题是如何显示一条吐司消息说错误的用户名/密码?
目前它在登录失败期间不显示任何内容。此外,如何在成功登录后将屏幕从 activity_login.xml 更改为 login_page.xml?我应该添加什么代码?
登录活动.java:
package com.finchvpn.androidcloudpark;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Objects;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
private EditText textUsername;
private EditText txtPassword;
private static RestClient restClient = new RestClient();
private SharedPreferences.Editor sharedPreferencesEditor;
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
} catch (Exception e) {
}
textUsername = findViewById(R.id.textUsername);
txtPassword = findViewById(R.id.textPassword);
SharedPreferences sharedPreferences = getSharedPreferences("UserInfo", 0);
sharedPreferencesEditor = sharedPreferences.edit();
textUsername.setText(sharedPreferences.getString("textUsername", ""));
txtPassword.setText(sharedPreferences.getString("txtPassword", ""));
}
public static RestClient getRestClient() {
return restClient;
}
public void loginButtonClick(View v) {
if (!textUsername.getText().toString().equals("") && !txtPassword.getText().toString().equals("")) {
apiPostLogin(Constants.ANDROID_KEY + ":" + textUsername.getText().toString() + ":" + txtPassword.getText().toString());
sharedPreferencesEditor.putString("textUsername", textUsername.getText().toString());
sharedPreferencesEditor.putString("txtPassword", txtPassword.getText().toString());
sharedPreferencesEditor.commit();
} else {
Toast.makeText(LoginActivity.this, "NULL", Toast.LENGTH_LONG).show();
}
}
private void apiPostLogin(String data) {
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Logging in");
progress.setMessage("Please wait ...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
Call<ResponseBody> call = getRestClient().getLoginService().postLogin(data);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful() && response.body() != null) {
try {
String data = response.body().string();
JSONObject jsonObject = new JSONObject(data);
Constants.uid = Integer.parseInt(jsonObject.getString("id"));
Constants.username = jsonObject.getString("username");
Constants.email = jsonObject.getString("email");
Constants.credit = jsonObject.getString("credit");
Constants.qr_code = jsonObject.getString("qr_code");
Constants.created_at = jsonObject.getString("created_at");
Constants.updated_at = jsonObject.getString("updated_at");
Toast.makeText(LoginActivity.this, "apiPostLogin onResponse <<<< \r\n\r\n" + jsonObject.toString(), Toast.LENGTH_LONG).show();
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
progress.dismiss();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Incorrect username/password, please try again." + t.getMessage(), Toast.LENGTH_LONG).show();
progress.dismiss();
}
});
}
}
解决方案
首先,要更改活动布局,您必须在 onCreate 方法中更改这行代码:
setContentView(R.layout.activity_login);
其次,要在登录失败时显示 toast,请将您的 apiPostLogin 方法更改为:
private void apiPostLogin(String data) {
final ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Logging in");
progress.setMessage("Please wait ...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
Call<ResponseBody> call = getRestClient().getLoginService().postLogin(data);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful() && response.body() != null) {
try {
String data = response.body().string();
JSONObject jsonObject = new JSONObject(data);
Constants.uid = Integer.parseInt(jsonObject.getString("id"));
Constants.username = jsonObject.getString("username");
Constants.email = jsonObject.getString("email");
Constants.credit = jsonObject.getString("credit");
Constants.qr_code = jsonObject.getString("qr_code");
Constants.created_at = jsonObject.getString("created_at");
Constants.updated_at = jsonObject.getString("updated_at");
Toast.makeText(LoginActivity.this, "apiPostLogin onResponse <<<< \r\n\r\n" + jsonObject.toString(), Toast.LENGTH_LONG).show();
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
} else {
//
//
//This scope runs where the login fails
}
progress.dismiss();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Incorrect username/password, please try again." + t.getMessage(), Toast.LENGTH_LONG).show();
progress.dismiss();
}
});
}
推荐阅读
- bash - Bash脚本在动态变量上更改部分文件夹路径
- python - 尝试将 pandas 数据框导出为 excel 文件时出错
- javascript - 如何使用 NodeJS 和 Yahoo! 发出 POST 请求 梦幻体育API
- c++ - ',' 标记之前应为 ')' - SFML 创建 RectangleShape
- python - 使用 N 个工作人员执行多处理任务列表的最佳方法?
- algorithm - 水壶问题GCD解决方法解释
- javascript - Puppeteer 无法在特定的输入字段中写入
- java - 在 JMH 中的每个基准测试之后恢复 Java 列表状态
- react-native - ReactNative:使用 Axios 将图像文件上传到服务器不起作用
- c++ - macosx 上信号处理程序中的 rip 寄存器值