activity_main.xmlactivity_2.xmlMainActivity.javaActivity2.javaAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.application.exampleapp.MainActivity"> <EditText android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" /> <EditText android:id="@+id/edittext2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/edittext1" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:inputType="number" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Activity 1" android:textSize="30sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView3" android:layout_centerHorizontal="true" android:text="open activity 2" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.application.exampleapp.Activity2"> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" /> <TextView android:id="@+id/textview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/textview1" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Activity 2" android:textSize="30sp" /> </RelativeLayout>
package com.example.application.exampleapp; 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 MainActivity extends AppCompatActivity { public static final String EXTRA_TEXT = "com.example.application.example.EXTRA_TEXT"; public static final String EXTRA_NUMBER = "com.example.application.example.EXTRA_NUMBER"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openActivity2(); } }); } public void openActivity2() { EditText editText1 = (EditText) findViewById(R.id.edittext1); String text = editText1.getText().toString(); EditText editText2 = (EditText) findViewById(R.id.edittext2); int number = Integer.parseInt(editText2.getText().toString()); Intent intent = new Intent(this, Activity2.class); intent.putExtra(EXTRA_TEXT, text); intent.putExtra(EXTRA_NUMBER, number); startActivity(intent); } }
package com.example.application.exampleapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_2); Intent intent = getIntent(); String text = intent.getStringExtra(MainActivity.EXTRA_TEXT); int number = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0); TextView textView1 = (TextView) findViewById(R.id.textview1); TextView textView2 = (TextView) findViewById(R.id.textview2); textView1.setText(text); textView2.setText("" + number); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.application.exampleapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity2" android:parentActivityName=".MainActivity"></activity> </application> </manifest>