pass values from one screen to another?

2019-02-27 18:53发布

I need to pass data's (String) from one screen to another screen. At the time of button click i need to pass values from first screen to the next screen.

3条回答
够拽才男人
2楼-- · 2019-02-27 19:00

Register an onClickListener for the button and pass the required data by adding it to the Intent.

Button button = (Button) findViewById(R.id.button);
 button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                            Intent intent = new Intent(Activity1.this, Activity2.class); 
                            intent.putExtra("extra", data);
                            startActivity(intent); 
             });  

You can get the data in Activity2 by

String extra = getIntent().getStringExtra("extra"); 
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-27 19:02

You can pass along data as extras in the intent that starts the second activity:

Intent myIntent = new Intent(view.getContext(), NextActivity.class); 
myIntent.putExtra("extra", id);
startActivityForResult(myIntent, 0);   

In the oncreate method of your profile activity you can access the extras:

int id = getIntent().getStringExtra("extra");       

If you are new to Android, it might help to read through the examples in the developer docs, like the notepad tutorial.

查看更多
该账号已被封号
4楼-- · 2019-02-27 19:18

A better way to do it (since you can access this value from anywhere) might be using sharedPrefrences. But that would depned on your application.

http://developer.android.com/reference/android/content/SharedPreferences.html

查看更多
登录 后发表回答