-2

我正在制作一个程序,其中我需要使用活动之间的 Intent将值从一个活动传递到另一个活动。

所以这是我的问题How to Pass and Get an Intent into another activity

请参阅下面的代码,使用按钮单击我想将意图传递给另一个活动..

FirstActivity.java:

 public class FirstActivity extends Activity {
// Initializing variables
EditText inputName;
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

    inputName = (EditText) findViewById(R.id.name);
    Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);

    //Listening to button event
    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Code to start a new Intent and to pass value


        }
    });
}
4

5 回答 5

5

这里没什么难的,您只需使用以下代码将名称的值从 FirstActivity 传递给 SecondActivity:

    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);

            //Sending data to another Activity
            nextScreen.putExtra("name", inputName.getText().toString());
            startActivity(nextScreen);

        }
    });
}

并在第二个活动中使用 onCreate() 方法中的以下代码:

 TextView txtName = (TextView) findViewById(R.id.txtName);     
 Intent i = getIntent();
    // Receiving the Data

    String name = i.getStringExtra("name");
    txtName.setText(name);
于 2013-04-17T05:38:29.083 回答
1

是的, Ketlyen,我同意@klamitsuri

您只需要像他在代码中显示的那样传递值:

  Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);

        //Sending data to another Activity
        nextScreen.putExtra("name", inputName.getText().toString());
        startActivity(nextScreen);

并简单地使用:

  Intent i = getIntent();
// Receiving the Data

String name = i.getStringExtra("name");
于 2013-04-17T06:16:51.583 回答
0

在你的第一个活动中

 btnNextScreen.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
       Intent i= new Intent(firstActivity.this,secondActivity.class);
       i.putExtra("key","mystring");// key and the value
       startActivity(i);

    }
  });
}

在你的第二个活动 onCreate()

setContentView(R.layout.second); 
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
 TextView tv= (TextView)findviewById(R.id.textView1);
 String value = extras.getString("key"); // get the value based on the key
 tv.setText(value); 
} 
于 2013-04-17T05:36:34.703 回答
0

在您当前的 Activity 中,创建一个新的 Intent:

Intent i = new Intent(FirstActivity.this, NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

然后在新的 Activity 中,检索这些值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}
于 2013-04-17T05:37:35.577 回答
0

您可以使用 startActivityForResult(intent,result); 传递值或数据。

并获取前一个活动发送的值覆盖函数 onActivityResult(int requestCode, int resultCode,Intent data);

例如:

 public class MyActivity extends Activity {
 ...

 static final int PICK_CONTACT_REQUEST = 0;

 protected boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
         // When the user center presses, let them pick a contact.
         startActivityForResult(
             new Intent(Intent.ACTION_PICK,
             new Uri("content://contacts")),
             PICK_CONTACT_REQUEST);
        return true;
     }
     return false;
 }

 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == PICK_CONTACT_REQUEST) {
         if (resultCode == RESULT_OK) {
             // A contact was picked.  Here we will just display it
             // to the user.
             startActivity(new Intent(Intent.ACTION_VIEW, data));
         }
     }
 }}
于 2013-04-17T05:43:11.677 回答