简体   繁体   中英

How to change a textViews visibility from another activities button click in android?

I have activity 1 with 3 invisible text views and a button that opens activity 2 with 3 edit texts and a submit button. How can i make the text view visible and the button invisible in activity 1 when pressing the submit button in activity 2?

I am starting activity 2 with startActivityForResult, when pressing the submit button on activity 2 it goes back to activity 1.

Code for activity 1

public class MainActivity extends AppCompatActivity {
private static final int ACTIVITY_2_RESULT_CODE = 0;
Button btn1;

// This method opens the rod_activity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);
    // Locate the button in activity_main.xml
    btn1 = (Button)findViewById(R.id.textView2);
    // Capture button clicks
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // Start SecondActivity.class for result
            Intent myIntent3 = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivityForResult(myIntent3, ACTIVITY_2_RESULT_CODE);
        }
    });
}
// This method is called when second activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Check that it is the second activity with an OK result
    if (requestCode == ACTIVITY_2_RESULT_CODE) {
        if (resultCode == RESULT_OK) {
            // Get string data from Intent
            String string1 = data.getStringExtra("@id/edittext1");
            // Set text view with string
            TextView textview1 = (TextView)findViewById(R.id.textview1);
            textview1.setText(string1);

activity 1 xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_marginTop="4dp"
    android:layout_marginLeft="10dp"
    android:textStyle="bold"
    android:text="@string/name"
    android:textSize="18sp"
    android:id="@+id/textView1"/>
<Button
    android:id="@+id/button1"
    android:layout_width="65dp"
    android:layout_height="50dp"
    android:layout_alignStart="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="16dp"
    android:text="@string/name1"
    android:textStyle="bold" />

Code for activity 2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view
    setContentView(R.layout.activity_second);
// Create the submit button
    btn1 = (Button)findViewById(R.id.bSubmit);
    btn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Get the text from EditText and put the string to pass back into an Intent
            EditText edittext1 = (EditText)findViewById(R.id.edittext1);
            String stringToPassBack = edittext1.getText().toString();
            Intent myIntent1 = getIntent();
            myIntent1.putExtra("@id/edittext1", stringToPassBack);

activity 2 xml

<EditText
    android:id="@+id/edittext1"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:maxLength="02"
    android:ems="10"
    android:hint="@string/name2"/>

<Button
    android:id="@+id/bSubmit"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:text="@string/submit"
    android:layout_centerHorizontal="true"/>

Declear 3 global string var in activity 1 and one as int

public String Stringvar1="";
public String Stringvar2="";
public String Stringvar3="";
public int intvar = 0;

when press submit sing value in these string variable and set int=1

in function Oncreate on activity 1

textview1.settext(Stringvar1);
textview2.settext(Stringvar2);
textview3.settext(Stringvar3);

and for visiblity of button

if (intvar == 1) { 
   button.setvisibality(View.VISIBLE);
}else { 
   button.setvisibality(View.INVISIBLE);
}

You can use a variable saved in Activity 2 Bundle. I have a simple example to give you an idea. The code is tested.

Activity one

public class Main1Activity extends AppCompatActivity {

private TextView one;
private TextView two;
private TextView three;

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

    one = (TextView)findViewById(R.id.one);
    two = (TextView)findViewById(R.id.two);
    three = (TextView)findViewById(R.id.three);

    one.setVisibility(View.GONE);
    two.setVisibility(View.GONE);
    three.setVisibility(View.GONE);

    Button goToActivity2 = (Button)findViewById(R.id.buttonOne);
    goToActivity2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gotoTwoIntent = new Intent(MainActivity1.this, MainActivity2.class);
            startActivity(gotoTwoIntent);
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    Bundle bundle = getIntent().getExtras();
    if(bundle != null){
        String fromTwo = bundle.getString("FROM_TWO");
        if(!TextUtils.isEmpty(fromTwo)){
            one.setText(bundle.getString("EDITTEXT_VALUE_ONE"));
            two.setText(bundle.getString("EDITTEXT_VALUE_TWO"));
            three.setText(bundle.getString("EDITTEXT_VALUE_THREE"));
        }
    }
  }
}

Activity Two

public class Main5Activity extends AppCompatActivity {

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

    final EditText one = (EditText)findViewById(R.id.one);
    final EditText two = (EditText)findViewById(R.id.two);
    final EditText three = (EditText)findViewById(R.id.three);

    Button backToOne = (Button)findViewById(R.id.button_two);
    backToOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String valueOne = one.getText().toString();
            String valueTwo = two.getText().toString();
            String valueThree = three.getText().toString();

            Intent backToOne = new Intent(MainActivity2.this, Main5Activity1.class);
            backToOne.putExtra("FROM_TWO", "1");
            backToOne.putExtra("EDITTEXT_VALUE_ONE", valueOne);
            backToOne.putExtra("EDITTEXT_VALUE_ONE", valueTwo);
            backToOne.putExtra("EDITTEXT_VALUE_ONE", valueThree);

            startActivity(backToOne);
        }
    });

  }
}

You're looking for OnActivityResult. When submit button in activity 2 is pressed use OnAcitivityResult to notify activity 1 and hide your button.

OnActivityResult

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM