
[英]How to display a welcome message including username from another activity in android?
[英]Android: How to grab username info after login and display welcome 'username' in MainActivity
我已经为此做了很多搜索,其中大多数不是 android。
我正在使用 sharedpref 将用户名保存在 session 中,直到注销。 我想在 mainactivity 中显示欢迎的“用户名”。
. 现在我想要一个示例代码,用于在保存在 sharedprefs 中的 mainactivity class 中获取“用户名”并将其显示在 textview 中。
下面是我的登录 class 打开 mainActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.commit();
if(prefs.getString("username", null)!=null)
{Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);}
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("1111")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
else if(username.equals("2222")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(i);
}
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close the application
finish();
}
}); }
MainActivity.java
public class MainActivity extends ListActivity
{
TextView selection;
CustomerListItem[] items = {
new CustomerListItem("Start Trip", StartTripActivity.class),
new CustomerListItem("Clock in", ClockinActivity.class),
new CustomerListItem("Log Out", LogoutActivity.class)};
private TextView resultsTxt;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.customer);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1, items));
selection = (TextView) findViewById(R.id.selection);
showname = (TextView) findViewById(R.id.showname);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this, items[position].getActivity());
startActivityForResult(intent, position);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK)
{
// Perform different actions based on from which activity is
// the application returning:
switch (requestCode)
{
case 0:
// TODO: handle the return of the
break;
case 1:
// TODO: handle the return of the
break;
case 2:
// TODO: handle the return of the
break;
default:
break;
}
}
else if (resultCode == RESULT_CANCELED)
{
resultsTxt.setText("Canceled");
}
}
}
尝试...
在您的登录活动中:
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
prefs.edit().putString("username", username).commit();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
在您的主要活动中...
public class MainActivity extends Activity {
private String username = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(...) here
SharedPreferences prefs = getSharedPreferences("MyApp", MODE_PRIVATE);
username = prefs.getString("username", "UNKNOWN");
...
}
}
首先,您应该将用户名作为额外的传递,以便下一个活动可以获取它。 把它放在你的登录活动中:
String username = prefs.getString("username");
Intent i = new Intent(this, MainActivity.class);
// this is where you should pass the username
i.putExtra("username", username);
startActivity(i);
之后,把它放在你的 MainActivity 中,可能在onCreate
方法中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainview);
Bundle extras = getIntent().getExtras();
if (extras.containsKey("username")) {
String username = extras.getString("username");
// put whatever code you want here to show the username
}
}
希望它能回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.