我如何传递一个值从一类到另一个 [英] How do I pass a value from one class to another

查看:117
本文介绍了我如何传递一个值从一类到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何传递一个值从一类到另一个。

How do I pass a value from one class to another.

例如

一类

        public String aButton(View view) { 
        startActivity(new Intent(this, ContentHolder.class));
        this.choice = "kick_boxing.txt";

        return choice;

    }

这会导致一个活动打开。当用户点击相关按钮将调用此方法。

This causes an activity to open. This method is called when the user clicks on the relevant button.

在这家新开业的活动,我想设置的字符串选择通过点击几个按钮中的一个进行。由于Java没有做多的扩展,我不能扩展活动,这个类,我想,我需要使用继承。

In this newly opened activity, I want to set the String to choice made by clicking one of several buttons. Since java doesn't do multiple extending, I can't extend the activity and this class, which I think I need in order to use inheritance.

所以,我能做些什么来传递从A级的选择B类?

So, what can I do to pass the choice from class A to Class B?

修改

            public String aButton(View view) { 
        Intent i = new Intent(getApplicationContext(), ContentHolder.class);
        i.putExtra("choice", choice);


        startActivity(new Intent(this, ContentHolder.class));
        this.choice = "kick_boxing.txt";

        return choice;

    }

和其他类,而只有在OnCreate(我认为这是主要的方法?)

and in the other class, just in the onCreate (I think of these as main methods?)

        public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Context m_Context = getApplicationContext();

    TextView textview = new TextView(this);
    textview.setText("This is the content view");
    setContentView(textview);

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) {
    String value = extras.getString(key);
    }



    InputStream input;

    try {
        input = m_Context.getAssets().open(fileName);

键不能得到解决。

key cannot be resolved.

推荐答案

易这样的..

在你当前活动,创建一个意图

in your current activity, create an intent

Intent i = new Intent(getApplicationContext(), ActivityB.class);
i.putExtra("choice", value_of_the_data);

startActivity(ⅰ); 然后在其他活动,检索这些值。

startActivity(i); then in the other activity, retrieve those values.

Bundle extras = getIntent().getExtras(); 
if(extras !=null) {
String value = extras.getString("choice");
}

这篇关于我如何传递一个值从一类到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆