我有一个Login按钮,按下后会将用户发送到另一个活动,并在TextView中发布用户名。那部分工作正常。问题是当我尝试将第二个活动TextView中的用户名传递给它没有显示的第三个活动TextView时。 android:提示显示为文本,所以我知道xml很好。我已经尝试过在这个网站上找到的各种“putExtra”意图。我想我错过了什么......帮助。我对android很新,所以嘲笑新手:)
ACTIVITY 2
public class DashboardActivity extends Activity {
UserFunctions userFunctions;
Button btnLogout;
Button scanButton;
Button userinfo;
Button help;
Button receipt;
TextView showmsg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Dashboard Screen for the application
* */
// Check login status in database
userFunctions = new UserFunctions();
if(userFunctions.isUserLoggedIn(getApplicationContext())){
setContentView(R.layout.dashboard);
scanButton = (Button) findViewById(R.id.scanButton);
btnLogout = (Button) findViewById(R.id.btnLogout);
userinfo = (Button) findViewById(R.id.userinfo);
help = (Button) findViewById(R.id.help);
receipt = (Button)findViewById(R.id.receipt);
showmsg = (TextView)findViewById(R.id.showmsg);
Intent in = getIntent();
if (in.getCharSequenceExtra("usr") != null) {
final TextView setmsg = (TextView)findViewById(R.id.showmsg);
setmsg.setText("User: "+in.getCharSequenceExtra("usr"));
}
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
userFunctions.logoutUser(getApplicationContext());
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
});
// Link to CC
scanButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
MyScanActivity.class);
startActivity(i);
finish();
}
});
// Create a customer receipt
receipt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(DashboardActivity.this, ReceiptActivity.class));
intent.putExtra("TextView",showmsg);
startActivity(intent);
finish();
}
});
// Link to User info
userinfo.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
UserInfoActivity.class);
startActivity(i);
finish();
}
});
// Link to Help info
help.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
HelpActivity.class);
startActivity(i);
finish();
}
});
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
}
答案 0 :(得分:1)
登录后,尝试将用户名保存在SharedPreferences
中,然后您可以随时随地获取该值:
保存用户名:
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
prefs.edit().putString("username", username).commit();
从yourActivity中检索用户名的值:
SharedPreferences prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
String username = prefs.getString("username");
yourTextView.setText(username);
答案 1 :(得分:1)
@SmulianJulian:
有一个错误。您将组件 TextView 传递给下一个意图,而您应该只传递它的文本。
您目前正在执行此操作:
intent.putExtra("TextView",showmsg);
但你应该像
那样做Bundle extras = myIntent.getExtras();
extras .putString("TextView",showmsg.getText().toString());
并且在第3个活动中你应该得到这样的值:
bundle = getIntent().getExtras();
bundle.getString("TextView");
然后将此值设置为textview。
TextView yourTV = ((TextView) findViewById(R.id.your_textview_id));
yourTV.setText(bundle.getString("TextView"));
希望它有所帮助。