开发者

i am trying to get the text in a Button from another Activity

开发者 https://www.devze.com 2023-02-19 23:41 出处:网络
I have a main activityand in this activity i have a bu开发者_StackOverflow中文版tton in this button i have a Text i went to get the text from another activity,what i should do to make that happen the

I have a main activity and in this activity i have a bu开发者_StackOverflow中文版tton in this button i have a Text i went to get the text from another activity,what i should do to make that happen the simple way to getText() dos't work.

thanks for helping


You can add params to your intent like this:

 Intent intent = new Intent(context, SecondActivity.class);
 intent.putExtra(SecondActivity.EXTRA_BUTTONTEXT, yourButtonText);
 context.startActivityForResult(intent, SecondActivity.SECONDACTIVITY_REQUEST);

And from the second activity, get the param like this:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Bundle extras = getIntent().getExtras();
      String yourButtonText = extras.getString(EXTRA_BUTTONTEXT);
      ...


I think you should use the Intent. Why do you need the text of the button in another activity?


You can use Shared Preference for Storage of data and use that data any where you want.

Take a look at this...


I'm not sure I've understood... Anyway, it's more less like firefox1986 explained. You must start activity for result, as you pass a value to another activity. If you want to change the label of the button sometime later or just have a beautiful code, then write TextView buttonLabel = (TextView)findViewById(R.id.button1); and then pass buttonLabel to Extras.

0

精彩评论

暂无评论...
验证码 换一张
取 消