Android learning data transfer

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Implementation requirements:

Create a project that includes two activities (that is, two interfaces) with the interface design shown below

(1) Click the corresponding text in the first interface to jump to the corresponding interface.

(2) Click “tongue twister” on the first screen to jump to the second screen with an explicit Intent.

(3) Click “Joke” or “Poker” in the first screen to jump to the second screen with an explicit intent.

(4) Click “Return” button in the second interface to return to the interface shown in FIG. 1

Implementation idea:

Click the text box, through the method in the event trigger, get the text information, intercept the string, get the prefix number string, and transfer the data.

Activity2 receives the data passed from Activity1, matches it, and sets the information in the TextView.

Example:

<TextView android:id="@+id/id1" android:layout_width="134dp" android:layout_height="51dp" android:clickable="true" android:gravity="center" android:onClick="transferAc" android:text="1. "Android :textSize="16sp" app: layOUT_constraintBottom_toendof ="parent" app:layout_constraintEnd_toEndOf="parent" App: layout_constraintHorizontal_bias = "0.498" app: layout_constraintStart_toStartOf = "parent" App: layout_constraintTop_toTopOf = "parent" app: layout_constraintVertical_bias = "0.344" / >Copy the code

Set text in TextView, and set clickable properties, click through transferAc method.

Set clickability;

 android:clickable="true"
Copy the code

The TxtView object can be obtained by view coercion, so that different TextViews can get the current component information through the same click event.

 public void transferAc(View view) {
         TextView textView = (TextView) view;
         String str = textView.getText().toString().split("\\.")[0];
         Intent intent = new Intent(MainActivity.this, MainActivity2.class);
         System.out.println(str+"--------------");
         if(str.equals("1")){
             intent.putExtra("1",str);
         }else if(str.equals("2")){
             intent.putExtra("1",str);
         }else if(str.equals("3")){
             intent.putExtra("1",str);
         }else if(str.equals("4")){
             intent.putExtra("1",str);
         }else if(str.equals("5")){
             intent.putExtra("1",str);
         }
         startActivity(intent);
     }
Copy the code

Activity2 receives and sets:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); String id = intent.getStringExtra("1"); TextView view = findViewById(R.id.textView5); If (id.equals("1")){view.settext (" ash "); if(id.equals("1")){view.settext (" ash "); }else if(id.equals("2")){view.settext (" There is a child whose last name is Wang, this joke is so long "); }else if(id.equals("3")){view.settext ("3"); Playing CARDS "); View.append ("\n this feature to be developed..." ); }else if(id.equals("4")){view.settext ("4"); The browser "); View.append ("\n this feature to be developed..." ); }else if(id.equals("5")){view.settext ("5"); Called "); View.append ("\n this feature to be developed..." ); }else{view.settext (" input error "); }}Copy the code

Effect:

The end:

If you see this or happen to help you, please click 👍 or ⭐ thank you;

There are mistakes, welcome to point out in the comments, the author will see the modification.