输入一个活动的文本并将其显示在另一个活动中,而不创建另一个intent或startActivity()方法

时间:2016-12-12 10:36:07

标签: java android xml android-intent textview

我是android新手并开始探索事物。我的主要活动是 FirstActivity 和相应的 first_layout.xml 。每当点击第三个活动按钮时,我们最终会链接到 thirdActivity.java 链接到 ThirdActivity.java ,其中包含一个EditText视图组件,我们通过该组件输入数据并在点击时发送我正在尝试将文字 THIRD 更改为输入的数据。请注意,我不想通过意图启动新活动或使用startActivity(),因为它会更改FirstActivity的状态 ,点击点击按钮,消息 Hello World!更改为问候,然后如果我打开ThirdActivity和发送数据 Greetings 的状态应保持原样,但如果我使用intent或startActivity(),则会覆盖问候状态并显示第一个即 Hello World!我不想在这里不断尝试不同的方法,非常感谢你的帮助。

我有FirstActivity和ThirdActivity的代码及其截图,如下所示。提前谢谢。

FirstActivity.java

public class FirstActivity extends AppCompatActivity {

    TextView textViewX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        textViewX= (TextView) findViewById(R.id.f_textView_1);
    }

    public void toggleMessage(View view){

        if(textViewX.getText().equals("Hello World!")){
            textViewX.setText("Greetings");
        }

        else if(textViewX.getText().equals("Greetings")){
            textViewX.setText("Hello World!");
        }

    }

    public void toggleActivities(View view){

        Intent intent = null;

        if( ((Button )view).getText().equals("Second Activity")){
            intent = new Intent(this,SecondActivity.class);
        }

        else if(((Button)view).getText().equals("Third Activity")){
            intent = new Intent(this,ThirdActivity.class);
        }

        startActivity(intent);

    }

}

first_layout.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/first_layout"
    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.badarikrishnasg.firstapplication.FirstActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/f_textView_1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/f_button_1"
        android:layout_below="@+id/f_textView_1"
        android:text="Click"
        android:onClick="toggleMessage"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Move between activities"
        android:id="@+id/f_textView_2"
        android:layout_below="@+id/f_button_1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/f_button_2"
        android:text="Second Activity"
        android:onClick="toggleActivities"
        android:layout_below="@+id/f_textView_2"
        android:layout_alignParentStart="true"
        android:layout_marginTop="33dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/f_button_3"
        android:text="Third Activity"
        android:onClick="toggleActivities"
        android:layout_marginTop="28dp"
        android:layout_below="@+id/f_button_2"
        android:layout_alignParentStart="true" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:id="@+id/f_textView_3"
        android:text="THIRD"
        android:layout_below="@+id/f_button_3" />


</RelativeLayout>

Image of FirstActivity Layout

ThirdActivity.java

public class ThirdActivity extends Activity{

    TextView textview= null;
    EditText message_text=null;
    FirstActivity firstActivity;


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

    }

    public void sendMessage(View view){


        message_text= (EditText) findViewById(R.id.editText);
        String message = message_text.getText().toString();

        firstActivity.displayMessage(message);

        LayoutInflater factory = getLayoutInflater();  
        View textEntryView = factory.inflate(R.layout.first_layout, null);                       textview = (TextView) textEntryView.findViewById(R.id.f_textView_3);

        if(message!=null){
            if(textview.getVisibility()==View.VISIBLE){
                textview.setText(message);
            }
        }
        else{
            message="no Entry Found";
            textview.setText(message);
        }


        this.finish();
    }
}

third_layout.xml

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

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Enter Search word"
        android:id="@+id/editText" />

    <!--<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Third Activity"
        android:textColor="@color/colorAccent"
        android:textAlignment="center"
        android:layout_gravity="bottom"
        android:id="@+id/t_textView_1" />-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/send"
        android:onClick="sendMessage"/>

</LinearLayout>

Image of Third Activity Layout

3 个答案:

答案 0 :(得分:0)

您可以尝试使用startActivityForResult(intent);

开始第三项活动

https://developer.android.com/training/basics/intents/result.html

答案 1 :(得分:0)

你必须像这样编写静态方法displayMessage

public static void displayMessage (String msg){              
            textViewX.setText(msg);              
    }

并在发送按钮点击监听器中调用它

 message_text= (EditText) findViewById(R.id.editText);
 String message = message_text.getText().toString();

 firstActivity.displayMessage(message);

答案 2 :(得分:0)

为此你必须使用startActivityForResult(intent);

在FirstActivity中:

public void toggleActivities(View view){

        Intent intent = null;

        if( ((Button )view).getText().equals("Second Activity")){
            intent = new Intent(this,SecondActivity.class);
            startActivityForResult(intent,0);

        }

        else if(((Button)view).getText().equals("Third Activity")){
            intent = new Intent(this,ThirdActivity.class);
startActivityForResult(intent,1);
        }


    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
      if(resultcode = RESULT_OK){
         if(requestCode == 0){
           final String message = data.getStringExtra("key_message");
            textview.setText(message);
         }else if(requestCode == 1){
           final String message = data.getStringExtra("key_message");
            textview.setText(message);
         }
       }

    }

在SecondActivity中:

 public void sendMessage(View view){
        message_text= (EditText) findViewById(R.id.editText);
        String message = message_text.getText().toString();

         final Intent intent = new Intent();
intent.putExtra("key_message",message);
        setResult(RESULT_OK,intent)
        this.finish();
    }

在ThirdActivity中:

 public void sendMessage(View view){
        message_text= (EditText) findViewById(R.id.editText);
        String message = message_text.getText().toString();

         final Intent intent = new Intent();
intent.putExtra("key_message",message);
        setResult(RESULT_OK,intent)
        this.finish();
    }