Open class
MainActivity, we will pass text, number, array to class second.
Copy to
above Override
int number=2018;
String a="Year
passed is";
String[] fru
= { "Pear","Banana",
"Cashew", "Orange",
"Water melon", "Peach",
"Grape", "Mango",
"Plum" };
In button to open new class, add this code.
Intent
in=new
Intent(getBaseContext(),second.class);
Bundle bun = new Bundle();
bun.putString("text",a );
bun.putInt("num", number);
bun.putStringArray("gi", fru);
in.putExtras(bun);
Let look at
keyword we use, in class second we must use the same keyword to receive data.
In class
second, file ac2.xml has three
textViews.
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#800000"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#800000"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#800000"
android:textSize="20sp"
/>
Find address for textViews.
tv = (TextView)
findViewById(R.id.tv);
tv2 = (TextView)
findViewById(R.id.tv2);
tv3 = (TextView)
findViewById(R.id.tv3);
Declare an
array to receive data.
String[] array
= new String[9];
Copy to below findViewById.
Intent in = getIntent();
Bundle bun = in.getExtras();
int number =
bun.getInt("num");
String text = bun.getString("text");
array=bun.getStringArray("gi");
tv.setText(text+"");
tv2.setText(number+"");
tv3.setText("First
element is: "+array[0]);
Run project, press button to open class
second.
No comments:
Post a Comment