简体   繁体   中英

how to set text from another activity class

i have been stuck on this problem for far too long. i think it's simple but i'm a newbie android developer so your help would be greatly appreciated.

i have two activities. my main activity layout has a textview and a button. What i am trying to do is to implement the button to change the text of the textview from another activity class (Btn class).

this is my Btn class

public class Btn extends Activity implements OnClickListener {
    Button btn;
    TextView textBox;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.btn);  
        textBox = (TextView)findViewById(R.id.address);
        btn.setOnClickListener(this);           
    }

    @Override
    public void onClick(View v) {
        textBox.setText("Test Test");
    }

}

now this compiles and works fine but when i press the button nothing happens. i would like to know how change the text of the textview when i press the button. i know how to do this in my main activity class but i would like to do it from another class.

Please try implementing the below code,

MainActivity.java

    public class MainActivity extends Activity implements OnClickListener {

    Button btn;
    TextView textBox;

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        btn = (Button) findViewById(R.id.btn);  
        textBox = (TextView)findViewById(R.id.address);
        btn.setOnClickListener(this);           
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn:
            String strTemp = "Test Test";
            textBox.setText(strTemp);

            Intent intent = new Intent(MainActivity.this, Second.class);
            intent.putExtra("tempstring", strTemp);
            startActivity(intent);
            break;

        default:
            break;
        }

    }
}

Second.java

 public class Second extends Activity {

    private TextView txtTemp;

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

        txtTemp = (TextView) findViewById(R.id.txt_temp);

        Intent intent = getIntent();
        String strTemp = intent.getStringExtra("tempstring");

        txtTemp.setText(strTemp);
    }
}

Hope it works for you.

Even if you are really a complete newbie, the second thing to learn after Activity is Intent. There's no need for one Activity to retrieve objects' data and properties from another and send changes straight to them, it's not safe for the runtime because you can't be 100% sure the background Activity isn't killed by system. So Intents are invented for calling Activities and passing data to them. The code provided by Android Coders works in this way. First, you create an Intent object, put extra data (such as text you want to set to another Activity's TextView) and finally start an Activity responsible for this kind of Intent. In this code, Intent is explicit, which means that a certain Activity of certain package should be called. In this Activity, there's a code that checks up if there was an Intent called, reads extra data (your text) from it and finally calls setText(). There are also implicit Intents, based on the type of data and action. It's up to the system to give you a choise between all Activities from all packages present on the device, which can handle the type of data you provide to the Intent. Nothing personal, but if you won't be able to understand Intents, you won't understand Android. Try this very hard.

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