如何在 Android Studio 中将用户名传递给另一个 Activity

How do I pass an Username to another Activity in Android Studio

我目前正在编写我的第一个应用程序,我想用他们的名字问候用户。在主要活动中,我创建了一个名为 textview_username 的 Textview,我想 "fill" 使用用户登录名。我尝试使用 sharedPrefrences,但当我打开 MainActivity 时应用程序一直崩溃。

这是注册页面。

public class RegisterActivity 扩展 AppCompatActivity 实现 View.OnClickListener{ EditText用户名、密码; 按钮显示、注册、删除;

public static final String myprefnm= "Nickname";
public static final String myprefpw = "pass";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    username = findViewById(R.id.txte_username);
    password = findViewById(R.id.txte_password);
    show = findViewById(R.id.btn_show);
    register = findViewById(R.id.btn_register);
    delete = findViewById(R.id.btn_delete);
    show.setOnClickListener((View.OnClickListener)this);
    register.setOnClickListener((View.OnClickListener)this);
    delete.setOnClickListener((View.OnClickListener)this);
}

@Override
public void onClick(View v) {

    if(v == show){


        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(myprefnm, 0);
        SharedPreferences sharedPreferencespw = getApplicationContext().getSharedPreferences(myprefpw, 0);


        String usrname = sharedPreferences.getString(myprefnm, "1");


        username.setText(usrname);
        String pass = sharedPreferencespw.getString(myprefpw, "2");
        password.setText(pass);
        Toast.makeText(this, "Daten werden angezeigt",Toast.LENGTH_SHORT).show();
    }
    if(v == register){

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(myprefnm, 0);
        SharedPreferences sharedPreferencespw = getApplicationContext().getSharedPreferences(myprefpw, 0);


        SharedPreferences.Editor editor = sharedPreferences.edit();
        SharedPreferences.Editor editorpw = sharedPreferencespw.edit();


        editor.putString(myprefnm, username.getText().toString());
        editorpw.putString(myprefpw, password.getText().toString());


        editor.commit();
        editorpw.commit();
        Toast.makeText(this, "Registrierung erfolgreich", Toast.LENGTH_SHORT).show();
        Intent openSetPin = new Intent(RegisterActivity.this, SetPinActivity.class);
        startActivity(openSetPin);
    }


    if (v==delete){
        username.setText("");
        password.setText("");
        Toast.makeText(this, "Eingaben gelöscht", Toast.LENGTH_SHORT).show();
    }
}





public class MainActivity extends AppCompatActivity {
public static final String myprefnm= "Nickname";
// Variables
TextView username;
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(myprefnm, 0);
float x1, x2, y1, y2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String username = sharedPrefs.getString(myprefnm, "1");
    this.textview_username.setText(username);
    }
}

我使用了 Try 和 Catch,所以我认为错误是由共享首选项引起的。

您可以通过 Intent 将数据从一个 activity 传递到另一个。 您只需要数据本身和 一个指向第一个的键

Intent openSetPin = new Intent(RegisterActivity.this, SetPinActivity.class);
String name =  username.getText().toString();
// Add the data to the intent using the a key
openSetPin.putExtra("name_key", name);
startActivity(openSetPin);

在另一边 (SetPinActivity),您可以使用 相同的密钥 提取数据,如下所示,

// Getting the intent which started this activity
Intent intent = getIntent();
// Get the data of the activity providing the same key value
String name = intent.getStringExtra("name_key");

您可以使用 SharedPreferences 实现相同的效果,但这会保留数据(将文件保存在磁盘上),您在这里不需要它。

希望您在 AndroidManifest.xml 文件中添加了全部 activity。 使用 Intent 在活动之间传递数据而不是 sharedprefs。正如@Themelis

所建议的