我有一个名为 accountinfo 的类,我想在用户登录后显示用户名。
public class accountinfo extends AppCompatActivity {
TextView name;
TextView nameTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accountinfo);
nameTV=(TextView)findViewById(R.id.name);
name=(TextView) findViewById(R.id.email);
nameTV.setText("Welcome " + name);
}
}
这将返回“欢迎空”
邮箱是数据库中存储的用户名
最佳答案
如果登录成功,您可以通过sharedpreferences保存数据。将“名称”保存到您的共享首选项,然后检索它。当然。这很容易。随便看看
把它放在登录 Activity 中
userName = edtName.getText().toString();
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
prefs.edit().putString("name",userName).apply();
然后您需要像这样从共享首选项中检索数据:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String name = pres.getString("name", "");
因此您可以轻松发送任何数据。
关于java - 登录后如何在屏幕上显示用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49346009/