我想在用户登录到应用程序后显示已登录的用户详细信息。它将通过警报对话框显示。但是如何添加,我添加的地方我做不到。请帮我。这是我的登录活动代码。 nedd如何将登录信息传递到下一个活动以显示警报对话框。
package com.dairy.iconic;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.rudf.library.UpdateChecker;
import static com.dairy.iconic.Iconic.isNetworkStatusAvialable;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText Name = findViewById(R.id.username);
final EditText Password = findViewById(R.id.password);
final Button Login = findViewById(R.id.login);
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(Name.getText().toString(), Password.getText().toString());
}
});
}
private void validate(String username, String password) {
if(
(username.equals("1510633101")) && (password.equals("01571728906"))
|| (username.equals("1510533102")) && (password.equals("01762824252"))
|| (username.equals("14077127")) && (password.equals("01722347879"))
|| (username.equals("13027191")) && (password.equals("01558922366")))
{
Intent intent = new Intent(LoginActivity.this, Home.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), (getString(R.string.login_success)), Toast.LENGTH_LONG).cancel();
} else {
//User Logged in Failed
Toast.makeText(getApplicationContext(), (getString(R.string.login_unsuccess)), Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
您需要创建一个AlertDialog片段,然后显示它。这是Dialogs的文档- https://developer.android.com/guide/topics/ui/dialogs#java
类似的东西-
public class ShowUserInfo extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage('User information')
.setPositiveButton('OK', new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do whatever you want to happen when the user presses 'ok'
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
然后简单地称呼它
DialogFragment newFragment = new ShowUserInfo();
newFragment.show(getSupportFragmentManager(), "userInfo");