页面自检的数据传递
图一:
实现效果如下
图二:
代码实现
图一布局文件
------------------------------------------------------------
<?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:orientation="vertical" tools:context="com.example.g160628_android_12_activity.Main3Activity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main3_EditText" android:hint="输入要发送的文字"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/but_main3_Button" android:text="发送" android:onClick="JumpMain4" /> </LinearLayout>
图一activity
-----------------------------------------------------------
package com.example.g160628_android_12_activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Main3Activity extends AppCompatActivity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); //文本框 editText = (EditText) findViewById(R.id.et_main3_EditText); //按钮 Button button= (Button) findViewById(R.id.but_main3_Button); } //点击事件跳界面 public void JumpMain4(View view){ //得到EditText的值 String text=editText.getText().toString(); Intent intent=new Intent(this,Main4Activity.class); intent.putExtra("text",text); startActivity(intent); } }
图二布局文件
----------------------------------------------------------
<?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:orientation="vertical" tools:context="com.example.g160628_android_12_activity.Main4Activity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/et_main4_EditText" android:textSize="25dp"/> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上面接收后的值" android:textSize="25dp"/> </LinearLayout>
图二activity
---------------------------------------------
package com.example.g160628_android_12_activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.EditText; public class Main4Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); EditText Main4editText= (EditText) findViewById(R.id.et_main4_EditText); //获取值 String text=getIntent().getStringExtra("text"); Main4editText.setText(text); } }