用意图传递数据

时间:2013-04-09 04:09:49

标签: android android-intent

为什么这不起作用?只是尝试将可编辑的字符串从一个活动传递到另一个活动。但我无法让它发挥作用。通过意图传递的字符串是否必须预先定义?如果是这样,我如何传递可编辑的字符串?

   @Override
  public void onClick(View v) 
     {
             // TODO Auto-generated method stub
         textOut.setText(textIn.getText());

            Intent intent = new  Intent(TutorialOne.this,MainActivity.class);
        intent.putExtra("text", textIn.getText());
        startActivity(intent);
     }


public class MainActivity extends Activity 
  {

TextView gettext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gettext = (TextView) findViewById(R.id.textView1);
    Intent intent = getIntent();
    intent.getExtras().getString("text");
    String s = intent.getStringExtra("text");
    gettext.setText(s);
    gettext.setTextColor(Color.WHITE);

}

3 个答案:

答案 0 :(得分:1)

使用此代替此。

 @Override
  public void onClick(View v) 
     {
             // TODO Auto-generated method stub
        textOut.setText(String.valueOf(textIn.getText()));
        Intent intent = new  Intent(TutorialOne.this,MainActivity.class);
        intent.putExtra("text", String.valueOf(textIn.getText()));
        startActivity(intent);
     }

而不是这个。

 @Override
  public void onClick(View v) 
     {
             // TODO Auto-generated method stub
         textOut.setText(textIn.getText());

            Intent intent = new  Intent(TutorialOne.this,MainActivity.class);
        intent.putExtra("text", textIn.getText());
        startActivity(intent);
     }

答案 1 :(得分:0)

尝试这样来获取String

    if (getIntent().getExtras() != null) {
        String  mstr = getIntent().getExtras().getString("text");               
        }

答案 2 :(得分:0)

@Override
  public void onClick(View v) 
     {
             // TODO Auto-generated method stub
        textOut.setText(String.valueOf(textIn.getText()));
        Intent intent = new  Intent(TutorialOne.this,MainActivity.class);
        String text = textIn.getText().toString().trim();
        intent.putExtra("text", text);
        startActivity(intent);
     }

使用此代码