有人能告诉我到底如何使用getExtra()和putExtra()的意图吗?实际上我有一个字符串变量str,它存储一些字符串数据。现在,我想把数据从一个活动发送到另一个活动。

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );

然后在SecondScreen.java中

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        TextView userName = (TextView)findViewById(R.id.userName);
        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("strName")!= null)
        {
            //TODO here get the string stored in the string variable and do 
            // setText() on userName 
        }

    }

我知道这是一个非常基本的问题,但不幸的是,我被困在这里了。 请帮助。

谢谢,

编辑:这里的字符串,我试图从一个屏幕传递到另一个是动态的。 也就是我有一个editText,在那里我获取字符串,不管用户类型是什么。然后在myEditText.getText(). tostring()的帮助下。我得到输入的值作为一个字符串,然后我必须传递这个数据。


当前回答

先放绳子

Intent secondIntent = new Intent(this, typeof(SecondActivity));
            secondIntent.PutExtra("message", "Greetings from MainActivity");

在此之后检索它

var message = this.Intent.GetStringExtra("message");

这就是全部;)

其他回答

更简单

发送页面

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
i.putExtra("id","string data");
startActivity(i)

接收方

Intent i = new Intent(SourceActiviti.this,TargetActivity.class);
String strData = i.getStringExtra("id");

使用这个来“放置”文件…

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

然后,要检索值,请尝试如下操作:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}

先放绳子

Intent secondIntent = new Intent(this, typeof(SecondActivity));
            secondIntent.PutExtra("message", "Greetings from MainActivity");

在此之后检索它

var message = this.Intent.GetStringExtra("message");

这就是全部;)

简单, 第一个活动-

    EditText name= (EditText) findViewById(R.id.editTextName);
    Button button= (Button) findViewById(R.id.buttonGo);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this,Main2Activity.class);
            i.putExtra("name",name.getText().toString());
           startActivity(i);
          }
    });

第二项活动-

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    TextView t = (TextView) findViewById(R.id.textView);
    Bundle bundle=getIntent().getExtras();
    String s=bundle.getString("name");
    t.setText(s);
}

如果需要,可以添加if/else条件。

一个小补充:你不需要为键创建自己的名字,android提供了这些,f.ex。Intent.EXTRA_TEXT。修改接受的答案:

Intent i = new Intent(FirstScreen.)这一点,SecondScreen.class); 字符串strName = null; i.putExtra(意图。EXTRA_TEXT strName); 然后,要检索值,请尝试如下操作: 字符串部份; Bundle extras = getIntent().getExtras(); If (extras == null) { 部份=零; }其他{ 部份= extras.getString (Intent.EXTRA_TEXT); }