0

我想获取在另一个活动中定义的 TextView 的值?我正在使用安卓工作室。

4

2 回答 2

0

您应该使用意图将值传递给其他活动。在第一个活动中:

   Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
   intent.putExtra("YOURKEY", yourTextViewValue);

   startActivity(intent);

访问下一个活动的意图

   String textViewValue= getIntent().getStringExtra("YOURKEY");
于 2018-10-07T08:34:41.910 回答
0

第一个活动(有 TextView)

SecondActivity.launchActivity(this, textView.getText().toString())

第二个活动(需要文本值)

public static void launchActivity(Context context, String textViewValue) {
    Intent intent = new Intent(context, SecondActivity.class);
    intent.putExtra("textValueKey", textViewValue)
    context.startActivity(intent);
}

@Override
public void onCreate(Bundle savedInstanceState) {
  if (getIntent() != null) {
      String textViewValue = getIntent().getStringExtra("textValueKey");
  }
  ...
}
于 2018-10-07T08:52:29.507 回答