如何在另一个活动[ANDROID]中显示文本?

时间:2017-02-11 22:43:48

标签: java android xml string android-intent

我正在尝试获取在一个活动中生成的文本,这个文本被连接起来,“MainActivity.java”,并使用另一个活动“DICS.java”在屏幕上显示它。我无法弄清楚如何做到这一点。

  

MainActivity.java

package com.example.tristan.a1p1n1;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {


    public final static String EXTRA_MESSAGE = "com.example.tristan.a1p1n1.MESSAGE";
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void sendMessage(View view)
    {
        Intent intent = new Intent(this, DICS.class);
        EditText editText = (EditText) findViewById(R.id.editText);
        EditText editText2 = (EditText) findViewById(R.id.editText2);
        String message = editText.getText().toString() + editText2.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);

    }}
  

activity_main.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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tristan.a1p1n1.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Enter a String"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/editText" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Enter a String"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignRight="@+id/editText"
        android:layout_alignEnd="@+id/editText"
        android:layout_marginRight="11dp"
        android:layout_marginEnd="11dp"
        android:layout_marginTop="13dp" />

    <Button
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="23dp"
        android:id="@+id/button"
        android:onClick="sendMessage" />
</RelativeLayout>
  

DICS.java

package com.example.tristan.a1p1n1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


/**
 * Created by Tristan on 2/11/2017.
 */
public class DICS extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dics);

    }
}
  

dics.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:text="@+id/sendMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView2" />
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

Intent intent = getIntent(); String dataGet = intent.getExtras()。getString(“EXTRA_MESSAGE”);

答案 1 :(得分:0)

获取意图,然后继续从意图中获取字符串

Intent intent = getIntent();
String stringFromIntent = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);