首先,你需要先建立两个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); } }
點擊查看更多內(nèi)容
為 TA 點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦