0

我希望从edittext字段中获取值并将其作为字符串传递给另一个活动。我已经完成了如下所示。我认为它可以工作,但我希望能够打印出文本以查看变量值是否实际传递

但是一旦我输入Log.d("PJM",value);了活动,就会出现错误

主屏幕活动

public class MainScreen extends Activity {

Button btnSearch, btnFavourites;
EditText enterDefinition;
Editable word;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    btnSearch=(Button)findViewById(R.id.btnSearch);
    btnFavourites=(Button)findViewById(R.id.btnFavourites);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);

    String text = enterDefinition.getText().toString();

    Intent i = new Intent(this, Definition.class);
    i.putExtra("KEY",text);
    //startActivity(i);

    //Intent i = new Intent(this, Definition.class);
    //i.putExtra("text_Label",theText);
    //startActivity(i);

定义活动

public class Definition extends Activity {

Button btnSend, btnSaveFave, btnReturn;
EditText enterDefinition;
String value;
TextView definition;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_definition);

    btnSend=(Button)findViewById(R.id.btnSend);
    btnSaveFave=(Button)findViewById(R.id.btnSaveFave);
    btnReturn=(Button)findViewById(R.id.btnReturn);
    enterDefinition=(EditText)findViewById(R.id.enterDefinition);
    definition=(TextView)findViewById(R.id.tvDefinition);

    definition.setText("some text");

    Bundle extras = getIntent().getExtras();
    String value = null; 
    if (extras != null) {
      value = extras.getString("KEY");
    }
    else {
      value = "None";
    }
    Log.d("text entered", value);
    Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
4

1 回答 1

1

你要:

String value = null; 
if (extras != null) {
  value = extras.getString("KEY");
}
else {
  value = "None"
}
Log.d("PJM", value);
于 2013-04-17T11:29:16.977 回答