Java 成功登录后是否更改屏幕?(安卓工作室)
在实习期间,我的任务是接管其他人的代码并处理一个项目。成功登录后,我在尝试导航到另一个屏幕时遇到困难。目前,成功登录后,下面的代码将我重定向回原始主菜单页(而失败的登录则不起任何作用) 我的问题是,如何显示一条吐司消息,上面写着错误的用户名/密码 当前,它在登录失败时不显示任何内容。此外,在成功登录后,如何将屏幕从activity_login.xml更改为landing_page.xml?我应该添加什么代码 LoginActivity.java:Java 成功登录后是否更改屏幕?(安卓工作室),java,android,android-studio,retrofit,Java,Android,Android Studio,Retrofit,在实习期间,我的任务是接管其他人的代码并处理一个项目。成功登录后,我在尝试导航到另一个屏幕时遇到困难。目前,成功登录后,下面的代码将我重定向回原始主菜单页(而失败的登录则不起任何作用) 我的问题是,如何显示一条吐司消息,上面写着错误的用户名/密码 当前,它在登录失败时不显示任何内容。此外,在成功登录后,如何将屏幕从activity_login.xml更改为landing_page.xml?我应该添加什么代码 LoginActivity.java: package com.finchvpn.and
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();
}
});
}
}
package com.finchvpn.androidcloudpark;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.view;
导入android.widget.EditText;
导入android.widget.Toast;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.util.Objects;
进口okhttp3.0响应电子书;
2.电话;;
2.回拨;
2.回应;;
公共类LoginActivity扩展了AppCompatActivity{
私有编辑文本文本用户名;
私有编辑文本txtPassword;
私有静态RestClient RestClient=new RestClient();
私有SharedReferences.Editor SharedReferencesEditor;
@SuppressLint(“提交引用”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
试一试{
Toolbar Toolbar=findviewbyd(R.id.Toolbar);
设置支持操作栏(工具栏);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}捕获(例外e){
}
textUsername=findViewById(R.id.textUsername);
txtPassword=findviewbyd(R.id.textPassword);
SharedReferences SharedReferences=GetSharedReferences(“用户信息”,0);
SharedReferencesEditor=SharedReferences.edit();
textUsername.setText(SharedReferences.getString(“textUsername”),“”);
setText(sharedPreferences.getString(“txtPassword”和“”);
}
公共静态RestClient getRestClient(){
返回客户;
}
公共无效登录按钮单击(视图v){
如果(!textUsername.getText().toString().equals(“”)&!txtPassword.getText().toString().equals(“”)&{
apiPostLogin(Constants.ANDROID_KEY+”:“+textUsername.getText().toString()+”:“+txtPassword.getText().toString());
SharedReferencesEditor.putString(“textUsername”,textUsername.getText().toString());
SharedReferencesEditor.putString(“txtPassword”,txtPassword.getText().toString());
SharedReferencesEditor.commit();
}否则{
Toast.makeText(LoginActivity.this,“NULL”,Toast.LENGTH_LONG.show();
}
}
私有void apiPostLogin(字符串数据){
最终进度对话框进度=新进度对话框(本);
progress.setTitle(“登录”);
progress.setMessage(“请稍候…”);
progress.setCancelable(false);//通过点击对话框外部禁用Disclose
progress.show();
Call Call=getRestClient().getLoginService().postLogin(数据);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()&&response.body()!=null){
试一试{
字符串数据=response.body().String();
JSONObject JSONObject=新的JSONObject(数据);
Constants.uid=Integer.parseInt(jsonObject.getString(“id”);
Constants.username=jsonObject.getString(“用户名”);
Constants.email=jsonObject.getString(“电子邮件”);
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首先,要更改活动布局,必须在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();
}
});
}
private void apipostlin(字符串数据){
最终进度对话框进度=新进度对话框(本);
progress.setTitle(“登录”);
progress.setMessage(“请稍候…”);
progress.setCancelable(false);//通过点击对话框外部禁用Disclose
progress.show();
Call Call=getRestClient().getLoginService().postLogin(数据);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()&&response.body()!=null){
试一试{
字符串数据=response.body().String();
JSONObject JSONObject=新的JSONObject(数据);
Constants.uid=Integer.parseInt(jsonObject.getString(“id”);
Constants.username=jsonObject.getString(“用户名”);
Constants.email=jsonObject.getString(“电子邮件”);
Constants.credit=jsonObject.getString(“credit”);
Constants.qr_code=jsonObject.getString(“qr_code”);
Constants.created_at=jsonObject.getString(“created_at”);
Constants.updated_位于
setResult(Activity.RESULT_CANCELED, returnIntent);
setResult(Activity.RESULT_OK, returnIntent);