首先,你需要先建立两个Activity,这里我们以MainActivtiy ,然后另一个是SecondActivtiy.
MainActivity中设置一个输入框跟一个点击按钮。然后SecondActivity中设置一个Text.
当在MainActivity写了东西之后,点击按钮。SecondActivity的Text就会显示MainActivity那里传送过来的内容。
MainActivty 的Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" tools:context=".MainActivity"> <EditText android:id="@+id/edt" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:minLines="3" android:maxLines="3" android:textColor="#000000" android:textSize="20dp" android:hint="请输入.." android:textColorHint="#707070"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击" android:textSize="15dp" android:textColor="#000000"/> </LinearLayout>
SecondActivtiy的Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" tools:context=".SecondActivity"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000000"/> </LinearLayout>
接下来,是MainActivity的java代码:
public class MainActivity extends AppCompatActivity { private Button btn; private EditText edt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = findViewById(R.id.btn); edt = findViewById(R.id.edt); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String data = edt.getText().toString(); Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("extra_data",data); startActivity(intent); } }); }
接着是SecondActivity的代码:
public class SecondActivity extends AppCompatActivity { private TextView txt1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); txt1 = findViewById(R.id.txt); Intent intent = getIntent(); String data = intent.getStringExtra("extra_data"); txt1.setText(data); } }