1.1 Build the first App

Send text in edit box to another page



 

 

activtiy_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/activity_main">

    <EditText
        android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:textAllCaps="false"
        android:onClick="sendMessage"/>
</LinearLayout>

activity_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content"
    tools:context="com.mycompany.myfirstapp.Content">

</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    /*
    In order for the newly launched activity to query the extra data. Define key as a public constant,
    Keys are usually defined using the application package name as a prefix, so that
    The keys are still guaranteed to be unique when interacting.
     */
    public final static String EXTRA_MESSAGE="com.mycompany.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage(View view){
        /*
        There are two parameters in this Intent constructor:
        The first parameter is Context (this is used because the current Activity is a subclass of Context)
        The second parameter is the Class of the application component that accepts the Intent sent by the system (in this case,
Refers to the activity to be started)
         */
        Intent intent=new Intent(this,Content.class);
        //Use the findViewById() method to get the EditText element
        EditText editText= (EditText) findViewById(R.id.edit_message);
        //Associate the text content of EditText to a local message variable, and use the putExtra() method to pass the value to the intent
        String message=editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE,message);
        /*
        Intents can carry key-value data types called extras.
        The putExtra() method takes the key name as the first parameter and the value as the second parameter
         */
        //Call startActivity to complete the startup of the new activity
        startActivity(intent);
    }
    //Run this method, the system will instantiate the Activity specified in the Intent after receiving our request

}

Content

/*
Every Activity is called through an Intent, no matter where the user navigates to.
We can get the Intent that started the activity and the data it contains by calling getIntent().
 */
public class Content extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_content);

        //Get the intent and assign it to a local variable
        Intent intent=getIntent();
        //Call getStringExtra to extract the message passed from MainActivity
        String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        //Create an object TextView
        TextView textView=new TextView(this);
        //Use setText() to set the text font size and content
        textView.setTextSize(40);
        textView.setText(message);

        RelativeLayout layout= (RelativeLayout) findViewById(R.id.content);
        layout.addView(textView);
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123585&siteId=291194637