是否有一种方式显示登录后的另一个活动的用户名?



我已经在android studio中创建了一个注册和登录页面,它工作得很好,但现在我想当用户登录时,他或她的用户名被捕获并显示在下一个活动中,这是主要活动。

这里是登录

public class Login extends AppCompatActivity {
TextInputEditText textInputEditTextUsername, textInputEditTextPassword;
Button ButtonLogin;
TextView textViewSignUp;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

textInputEditTextUsername = findViewById(R.id.username);
textInputEditTextPassword = findViewById(R.id.password);
ButtonLogin = findViewById(R.id.buttonLogin);
textViewSignUp = findViewById(R.id.signUpText);
progressBar = findViewById(R.id.progress);
textViewSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SignUp.class);
startActivity(intent);
finish();
}
});
ButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username;
String password;
TextView number1;

username = String.valueOf(textInputEditTextUsername.getText());
password = String.valueOf(textInputEditTextPassword.getText());
number1 = (TextView)findViewById(R.id.usernamelog);

if (!username.equals("") && !password.equals("")) {
//Start ProgressBar first (Set visibility VISIBLE)
progressBar.setVisibility(View.VISIBLE);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
//Starting Write and Read data with URL
//Creating array for parameters
String[] field = new String[2];
field[0] = "username";
field[1] = "password";
//Creating array for data
String[] data = new String[2];
data[0] = username;
data[1] = password;
PutData putData = new 
PutData("http://192.168.43.88/LoginRegister/login.php", "POST", field, data);
if (putData.startPut()) {
if (putData.onComplete()) {
progressBar.setVisibility(View.GONE);
String result = putData.getResult();
if (result.equals("Login Success")){
Toast.makeText(getApplicationContext(), result, 
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Login.this, 
MainActivity.class);
intent.putExtra("username","username");
startActivity(intent);
finish();
}
else{
Toast.makeText(getApplicationContext(), result, 
Toast.LENGTH_SHORT).show();
}
}
}
//End Write and Read data with URL
}
});
}
else {
Toast.makeText(getApplicationContext(),"All fields required", Toast.LENGTH_SHORT).show();
}
}
});
}

和MainActivity.java,我想要的用户名显示。

public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
SliderView sliderView;
TextView textView;
int[] images = {R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five,
R.drawable.six};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.usernamelog);
sliderView = findViewById(R.id.image_slider);
Intent intent = getIntent();
String username = intent.getStringExtra( "username");
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home ");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setUpToolbar();
navigationView = (NavigationView) findViewById(R.id.navigation_menu);
navigationView.setNavigationItemSelectedListener(new 
NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case  R.id.nav_home:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
case  R.id.nav_about:
Intent aboutIntent = new Intent(MainActivity.this, About.class);
startActivity(aboutIntent);
break;
case  R.id.nav_contact:
Intent contactIntent = new Intent(MainActivity.this, Contact.class);
startActivity(contactIntent);
break;
case  R.id.nav_faqs:
Intent faqsIntent = new Intent(MainActivity.this, FAQs.class);
startActivity(faqsIntent);
break;

case  R.id.nav_share:{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody =  "http://play.google.com/store/apps/detail?id=" + 
getPackageName();
String shareSub = "Try now";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
break;
case R.id.log:{
Intent logIntent = new Intent(MainActivity.this, Login.class);
startActivity(logIntent);
break;
}
}
return false;
}
});

CardView mainButton = (CardView) findViewById(R.id.StartUp);
CardView mainButton1 = (CardView) findViewById(R.id.siting);
CardView mainButton2 = (CardView) findViewById(R.id.farmplan);
CardView mainButton3 = (CardView) findViewById(R.id.cagedesign);
CardView mainButton4 = (CardView) findViewById(R.id.production);
CardView mainButton5 = (CardView) findViewById(R.id.marketing);
CardView mainButton6 = (CardView) findViewById(R.id.start);
ImageButton mainButton7 = (ImageButton) findViewById(R.id.log);
TextView number1 = (TextView) findViewById(R.id.usernamelog);

mainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ImageActivity.class);
startActivity(intent);
}
});
mainButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Siting.class);
startActivity(intent);
}
});
mainButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Farmplan.class);
startActivity(intent);
}
});
mainButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CageDesign.class);
startActivity(intent);
}
});
mainButton4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Production2.class);
startActivity(intent);
}
});
mainButton5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Marketing.class);
startActivity(intent);
}
});
mainButton6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, StartActivity.class);
startActivity(intent);
}
});

SliderAdapter sliderAdapter = new SliderAdapter(images);
sliderView.setSliderAdapter(sliderAdapter);
sliderView.setIndicatorAnimation(IndicatorAnimationType.WORM);
sliderView.setSliderTransformAnimation(SliderAnimations.DEPTHTRANSFORMATION);
sliderView.startAutoCycle();
}

请帮助我,让我可以上传我的应用程序在play商店。非常感谢

从php服务器返回所需的用户名作为响应的一部分。在这种情况下,您应该修改结果变量类型,从字符串到JSON对象。在Login Activity中把它作为一个额外的intent传递,就像这样

intent.putExtra("username",usernameResponse);

,usernameResponse是保存PHP服务器返回的首选用户名的变量。

显示php代码可以帮助更多!