我已经尝试了很多次并以不同的方式将字符串从一个活动(LoginActivity)传递到另一个活动(SettingsActivity)。当然,我做了很多研究,并尝试了每个提供的解决方案,但直到现在都无法帮助我
LoginActivity.class:
package com.naderk.pds;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import com.naderk.pds.contexts.ContextServerActivity;
import java.util.HashMap;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
final String LOG = "LoginActivity";
Button btnLogin, btnRegister;
EditText etUsername, etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnRegister = (Button) findViewById(R.id.bRegister);
btnLogin.setOnClickListener(this);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent seeTasksIntent= new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(seeTasksIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
HashMap postData = new HashMap();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
postData.put("txtUsername", username);
postData.put("txtPassword", password);
PostResponseAsyncTask task1 = new PostResponseAsyncTask(LoginActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(LOG, s);
if(s.contains("success")){
Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(LoginActivity.this, "Wrong username or password. Please register and try again.", Toast.LENGTH_LONG).show();
}
}
});
task1.execute("http://192.168.0.11/customer/");
visitactivity1();
}
public void visitactivity1() {
Intent i = new Intent(LoginActivity.this, SettingsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key", etUsername.getText().toString());
i.putExtras(bundle);
startActivity(i);
finish();
}
}
SettingsActivity.class:
package com.naderk.pds;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Intent;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
TextView textView = (TextView) findViewById(R.id.tvUserName);
Bundle bundle = getIntent().getExtras();
String stuff = bundle.getString("key");
textView.setText(stuff);
}
}
logcat的:
Exceeds 30.000 characters lol
我真的希望有人能够帮助我。
干杯!
答案 0 :(得分:1)
试试这个
public void visitactivity1() {
Intent i = new Intent(LoginActivity.this, SettingsActivity.class);
i.putExtra("key", etUsername.getText().toString());
startActivity(i);
finish();
}
以及第二项活动
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
TextView textView = (TextView) findViewById(R.id.tvUserName);
String stuff = getIntent().getStringExtra("key");
textView.setText(stuff);
}
}
希望它有所帮助(:
答案 1 :(得分:0)
您可以添加putExtras
if(s.contains("success")){
Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("passwordText", password.getText().toString());
intent.putExtra("usernameText",username.getText().toString());
startActivity(intent);
在第二个活动中,在onCreate()方法中实现以下内容
Intent intent = getIntent();
String password = intent.getStringExtra("passwordText");
String username = intent.getSringExtra("usernameText");
您可以阅读此博客,了解有关意图的更多信息:What are intents 总之是一个关于如何实现意图的教程。如果您想要更改结构,它可能会帮助您。
最后,我不确定这是否是您想要传递数据的方式:而是按我的建议使用它。我希望它有所帮助!
postData.put("txtUsername", username);
postData.put("txtPassword", password);
答案 2 :(得分:0)
您可以使用Bundle方法在Activity类中发送和接收字符串。