更改文本从一个活动到另一个文本

由网友(非我薄情)分享简介:我有跟做一个TextView改变文本时,我按下一个按钮,从另一个活动的麻烦,按钮从相同的活动作为自己在工作,但它会关闭我的应用程序,当我告诉它从另一个更改文本活动中,我的code是这样的:I having trouble with making a Textview change text when i push a...

我有跟做一个TextView改变文本时,我按下一个按钮,从另一个活动的麻烦,按钮从相同的活动作为自己在工作,但它会关闭我的应用程序,当我告诉它从另一个更改文本活动中,我的code是这样的:

I having trouble with making a Textview change text when i push a button from another activity, the button is working from within the same activity as its own but it shuts down my app as soon as I tell it to change text from another activity, my code looks like this:

这是从我的主要活动按钮code:

this is the button code from my main activity:

go = (Button)findViewById(R.id.go);
  go.setOnClickListener(new View.OnClickListener() 
  {

        public void onClick(View v) 
        {
            InventoryActivity.fire.setText("1 fire");

        }
    });

这是从与TextView的活动的code我希望改变:

and this is the code from the activity with the TextView I wish to change:

public class InventoryActivity extends Activity

{

public static TextView fire;


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


    fire = (TextView) findViewById(R.id.fire);
    }
}

再好,谢谢

推荐答案

不幸的是,你将无法从一个活动访问的另一项活动,在当前膨胀的布局是从第二个活动视图对象。 这里更多信息

Unfortunately, you wont be able to access view objects from one activity in another activity, when the current inflated layout is from second activity. More info here

我推荐的方法:假设InventoryActivity将启动SecondActivity,使用 startActivityForResult() onActivityResult()在InventoryActivity。在你的第二个活动中使用的setResult()与更新的文字额外的字符串参数。 Example这里

My recommended approach: Assuming InventoryActivity will start your SecondActivity, use startActivityForResult() and onActivityResult() in InventoryActivity. In your Second Activity use setResult() with a extra string parameter for the updated text. Example here

您可以在其他情况下使用共享preferences。

You could use shared preferences in other scenarios.

阅读全文

相关推荐

最新文章