1

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

   @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);

}
4

3 回答 3

1

使用 this 而不是 this。

 @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);
     }
于 2013-04-09T04:19:20.450 回答
0

像这样尝试获取字符串

    if (getIntent().getExtras() != null) {
        String  mstr = getIntent().getExtras().getString("text");               
        }
于 2013-04-09T04:46:21.810 回答
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);
     }

使用此代码

于 2013-04-09T04:51:07.777 回答