In Android, there are many ways to implement data transfer.
The following uses the characteristics of the java language to serialize the data through the Serializable interface and then pass it out.
1. Entity class:
Content.java:
import java.io.Serializable;
//利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。
//实体类
public class Content implements Serializable {
private int number;
private String name;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. Set data parameters:
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private EditText ed1,ed2;
private Button quary;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
quary = this.findViewById(R.id.quary);
ed1 = this.findViewById(R.id.ed1);
ed2 = this.findViewById(R.id.ed2);
quary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Content content = new Content();
String name = ed1.getText().toString();
content.setName(name);
int number = Integer.parseInt(ed2.getEditableText().toString());
content.setNumber(number);
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("serializable",content);
startActivity(intent);
}
});
}
}
Corresponding layout file:
activity_main.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed1"
android:hint="姓名:"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ed2"
android:inputType="number"
android:layout_below="@+id/ed1"
android:hint="年龄:"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/quary"
android:layout_below="@+id/ed2"
android:text="查看"/>
</RelativeLayout>
3. Receive parameters
MainActivity2.java:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.FileInputStream;
public class MainActivity2 extends AppCompatActivity {
private TextView xinxi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
xinxi = this.findViewById(R.id.tv);
Content content = (Content) getIntent().getSerializableExtra("serializable");
String result = "姓名"+content.getName()+"\n"+"年龄:"+content.getNumber();
xinxi.setText(result);
}
}
Corresponding layout file:
activity_main2.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textColor="@color/black"
android:textSize="20sp"
android:id="@+id/tv" />
</RelativeLayout>
Realize the effect:
Input data:
Click "View":
Simple and rude!
Thanks ლ(°◕‵ƹ′◕ლ)! ! !