Textview developed by Android uses intent to transfer information and realize the function example of registration interface

Time:2020-1-4

The example of this paper describes that textview developed by Android uses intent to transfer information and realize the function of registration interface. To share with you for your reference, as follows:

Use intent to pass values between activities

First of all, the main activity (after the personal information is written in the registration interface, click Register)

Jump to in activity (get information in mainactivity through intent)

The renderings are as follows:

Mainactivity implementation:

Java code:

public class Home extends AppCompatActivity {
  //For storing personal registration information
  EditText user_name ;
  EditText user_code ;
  EditText user_year ;
  EditText user_birth ;
  EditText user_phone ;
  //Click the registration button to jump
  Button button01 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Setcontentview (r.layout. Activity? Main); // display manlayout
    user_name = (EditText) findViewById(R.id.ed_name);
    user_code = (EditText) findViewById(R.id.ed_code);
    user_year = (EditText) findViewById(R.id.ed_year);
    user_birth = (EditText) findViewById(R.id.ed_birth);
    user_phone = (EditText) findViewById(R.id.ed_phone);
    button01 = (Button) findViewById(R.id.bn_01);
    //Information transfer between activities through intent
    button01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent01 = new Intent(Home.this,In.class);
        intent01.putExtra("name",user_name.getText().toString());
        intent01.putExtra("code",user_code.getText().toString());
        intent01.putExtra("year",user_year.getText().toString());
        intent01.putExtra("birth",user_birth.getText().toString());
        intent01.putExtra("phone",user_phone.getText().toString());
        startActivity(intent01);
      }
    });
  }
}

XML layout file:

<TableLayout
  android:id="@+id/root"
  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:stretchColumns="1">
  <TableRow>
    <TextView
      android:id="@+id/tv_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: text = "user name"
      android:textSize="16sp"/>
    <EditText
      android:id="@+id/ed_name"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: hint = "please fill in the login account"
      android:selectAllOnFocus="true"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: text = "password"
      android:textSize="16sp"/>
    <! -- Android: inputtype = "numberpassword" means only numeric password can be accepted -- >
    <EditText
      android:id="@+id/ed_code"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="numberPassword"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: text = "age"
      android:textSize="16sp"/>
    <! -- Android: inputtype = "numberpassword" indicates a numeric input box -- >
    <EditText
      android:id="@+id/ed_year"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="number"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: text = "birthday"
      android:textSize="16sp"/>
    <! -- Android: inputtype = "numberpassword" indicates the date input box -- >
    <EditText
      android:id="@+id/ed_birth"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="date"/>
  </TableRow>
  <TableRow>
    <TextView
      android:id="@+id/tv_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      Android: text = "phone number"
      android:textSize="16sp"/>
    <! -- Android: inputtype = "numberpassword" indicates the phone number input box -- >
    <EditText
      android:id="@+id/ed_phone"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:selectAllOnFocus="true"
      android:inputType="phone"/>
  </TableRow>
  <Button
    android:id="@+id/bn_01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    Android: text = "register" / >
</TableLayout>

In activities:

Java code:

public class In extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in);
    //Get data from mainactivity
    Intent intent01 = getIntent();
    //Place incoming information
    TextView textView01 = (TextView) findViewById(R.id.In_tv_01);
    textView01.setText( intent01.getStringExtra("name") + "\n"
        + intent01.getStringExtra("code") + "\n"
        + intent01.getStringExtra("year") + "\n"
        + intent01.getStringExtra("birth") + "\n"
        + intent01.getStringExtra("phone") );
  }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
  tools:context=".In">
  <! -- // place the information passed in by the previous activity -- >
  <TextView
    android:id="@+id/In_tv_01"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>

For more information about Android related content, readers can view our special topics: introduction and advanced course of Android development, summary of Android debugging skills and solutions to common problems, summary of usage of Android basic components, summary of Android view skills, summary of Android layout skills and summary of usage of Android controls

I hope this article is helpful for Android programming.