通过按下按钮将textview的值从一个活动传递到另一个活动

时间:2016-02-10 08:23:20

标签: android android-layout

这是我第一次在AndroidStudio工作。我想要做的是我想传递txresult(activity_main.xml)中生成的值,它是扫描后生成的值。我想在将Yes推送到我的SecondActivity.java之后传递该值,并且能够显示该值。

activity_main.xml中

             <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/LinearLayout1"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             android:weightSum="1">

             <View
                 android:layout_width="match_parent"
                 android:layout_height="1dp"
                 android:layout_marginTop="10dp"
                 android:layout_marginBottom="10dp"
                 android:background="#ddd" />

             <Button
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:text="Scan"
                 android:onClick="callZXing"
                 android:id="@+id/Button" />

             <View
                 android:layout_width="match_parent"
                 android:layout_height="1dp"
                 android:layout_marginTop="10dp"
                 android:layout_marginBottom="10dp"
                 android:background="#ddd" />

             <TextView
                 android:id="@+id/txResult"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:text="Rezultat:"
                 android:textSize="20sp" />

             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:textAppearance="?android:attr/textAppearanceLarge"
                 android:text="Is this the correct code?"
                 android:id="@+id/textView"
                 android:layout_marginTop="70dp"
                 android:layout_gravity="center_horizontal" />

             <Button
                 style="@style/CaptureTheme"
                 android:layout_width="112dp"
                 android:layout_height="68dp"
                 android:text="Yes"
                 android:layout_marginTop="30dp"
                 android:id="@+id/button2"
                 android:layout_weight="0.10"
                 android:layout_gravity="center_horizontal"
                 android:onClick="sendSecond" />

             <Button
                 style="@style/CaptureTheme"
                 android:layout_width="112dp"
                 android:layout_height="68dp"
                 android:text="No"
                 android:layout_marginTop="10dp"
                 android:id="@+id/button1"
                 android:layout_weight="0.10"
                 android:layout_gravity="center_horizontal"
                 android:onClick="callZXing" />

         </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {    
public static final int REQUEST_CODE = 0;   
private TextView txResult;

        public class FirstActivity extends Activity {

            @Override       
public void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

    findViewById(R.id.button2).setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                TextView textView = (TextView)findViewById(R.id.txResult);
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("textViewText", textView.getText().toString());
                        startActivity(intent);
                    }           
});         
}}

SecondActivity.java

   package br.exemplozxingintegration;

   import android.app.Activity; import android.os.Bundle; 
   import android.widget.TextView;

         public class SecondActivity extends Activity {


                 @Override
                 public void onCreate(Bundle savedInstanceState) {
                     super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_second);
                     Bundle extras = getIntent().getExtras();
                     if (extras != null) {
                         TextView textView = (TextView) findViewById(R.id.text_view);
                         textView.setText(extras.getString("textViewText"));
                     }
                 }}

Activity_second.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"
             android:weightSum="1">

             <TextView
                 android:id="@+id/text_view"
                 android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 android:layout_weight="0.53" /> </LinearLayout>

3 个答案:

答案 0 :(得分:0)

使用 <button onclick="goBack()">Go Back</button> <script> function goBack() { window.history.back(); } </script> 你可以做你想做的事。

Intent.putExtra()

您可以使用

获取值
    Intent intent = new Intent( this, MyActivity.class );
 intent.putExtra( "paramName", textview.getText().toString() ); 
    startActivity( intent );

您可以在第二项活动中使用此部分,特别是 Bundle extras = getIntent().getExtras(); if (extras != null) { String myParam=extras.getString("paramName"); } 方法。

答案 1 :(得分:0)

试试这个:

    Intent i=new Intent(MainActivity.this, SecondActivity.class);
    i.putExtra("result",txResult.getText());
    startActivity(i);

和你的SecondActivity:

Bundle extras = getIntent().getExtras(); 
if (extras != null) 
{
String strResult=extras.getString("result");
}

strResult会提供您之前的活动

传递的字符串

答案 2 :(得分:0)

FirstActivity.java

public class FirstActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_activity);

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView textView = (TextView) findViewById(R.id.text_view);
            Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtra("textViewText", textView.getText().toString());
            startActivity(intent);
        }
    });
}}

first_activity.xml

<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:id="@+id/text_view"
    android:text="123"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Text to Second Activity"/>
</LinearLayout>

SecondActivity.java

public class SecondActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText(extras.getString("textViewText"));
    }
}}

SecondActivity.xml

<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:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>