如何将用户名传递给 Android Studio 中的另一个活动 [英] How do I pass an Username to another Activity in Android Studio

查看:88
本文介绍了如何将用户名传递给 Android Studio 中的另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写我的第一个应用程序,我想通过他们的名字来问候用户.在 Main acitity 上,我创建了一个名为 textview_username 的 Textview,我想用用户登录名填充"它.我尝试使用 sharedPrefrences 但当我打开 MainActivity 时应用程序一直崩溃.

I'm currently writing my first App and I want to greet a User by their Name. On the Main acitity I created a Textview called textview_username which i want to "fill" with the users login name. I tried to work with sharedPrefrences but the App keeps crashing when I open the MainActivity.

那是注册页面.

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

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{ EditText username, password; Button show, register, delete;

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 and Catch,所以我认为错误是由共享首选项引起的.

I used Try and Catch so I think the Error is caused by the sharedpreferences.

推荐答案

您可以通过 Intent 将数据从一个活动传递到另一个活动.您所需要的只是数据本身和指向第一个的.

You can pass data from one activity to another through Intents. All you need is the data itself and a key which points to the first.

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),您可以使用相同的键提取数据,如下所示,

On the other side (SetPinActivity) you can extract the data using the same key as follows,

// 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 实现相同的效果,但这会保留数据(将文件保存在磁盘上),而您在此处不需要.

You may achieve the same using SharedPreferences but this persists the data (saves a file on the disk) and you don't need that here.

这篇关于如何将用户名传递给 Android Studio 中的另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆