从活动中传递价值的EditText在Android的Tabhost制表 [英] Passing EditText Value from an Activity to a Tab in Android Tabhost

查看:168
本文介绍了从活动中传递价值的EditText在Android的Tabhost制表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有tabhost包含两个选项卡(各为一个活动):添加新数据和我的数据。第一个显示的默认选项卡是添加新数据,因为它是排名第一的标签。

I have tabhost contains two tabs (each is an activity): "Add New Data" and "My Data". The default tab shown first is the "Add New Data" since it is the #1 tab.

要访问两个标签,我在第一次被记录。我想实现的是我想在登录活动用户名文本框的值传递到我的数据(第2个选项卡)选项​​卡。我大惑不解,因为我必须单击该选项卡我的数据,打开它,我对如何从登录类传递一个文本框的值标签(我的数据)。不知道

To access both tabs, I have to be logged in first. What I want to achieve is I want to pass the value of "Username" textfield in Login activity onto the "My Data" (2nd tab) tab. I'm quite bewildered since I have to click the tab "My Data" to open it and I have no idea on how to pass a textfield value from a Login class to the tab ("My Data").

如果您有关于如何实现这一目标的任何建议,请让我知道。

If you have any suggestion on how to achieve it, please let me know.

我已经通过论坛搜索,但没有找到任何合适的解决方案。
下面是在tabhost类(Dashboard.java)我的code:

I've searched through the forum but didnt find any suitable solution. Here is my code in the tabhost class (Dashboard.java):

public class Dashboard extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    TabHost host = getTabHost();
    host.addTab(host.newTabSpec("one").setIndicator("ADD NEW DATA")
            .setContent(new Intent(this, Input_form.class)));
    host.addTab(host.newTabSpec("two").setIndicator("MY DATA")
            .setContent(new Intent(this, MyData.class)));
}

// On Back Button Pressed

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        startActivity(new Intent(this, HomeLogin.class));           
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

有什么我可以在上面的code修改来实现我想要什么?如果是的话请告诉我,如果它不是通过这个code,请让我知道
谢谢

Is there anything I could modify in the code above to achieve what I want? If yes please tell me, or if it is not through this code, please let me know Thank you

推荐答案

同时读取多个教程和示例尝试后,最后,我来为这个问题的解决方案。我要在这里分享,让那些谁也有类似的问题可以克服它。

Finally after trying while reading several tutorials and examples, I've come to a solution for this problem. I am going to share it here so those who have similar problem can overcome it.

关键是我不需要传递文本字段值(其目的是用户名)。我只需要通过登录类,选择在数据库中的用户的ID并通过它通过一个意图的tabhost类。
要通过id来的标签,我做到了用 putExtra 在登录类是这样的:

The trick is I don't need to pass the textfield value (which was intended to be the username). I just need to select the id of the user in database through Login class and pass it through an intent to the tabhost class. To pass the id to the tabs, I did it by using putExtra in Login class like this:

Intent passId = new Intent(getApplicationContext(), Dashboard.class);
passId.putExtra(ID_USER, iduser);
startActivity(passId);

这是秘密,搭上额外的,我做到了像这样在我的tabhost类(我从这里阅读:<一href=\"http://stackoverflow.com/questions/3941869/how-to-pass-a-parameter-to-a-new-activity-with-tab-host\">How一个参数传递到与标签主机一个新的活动)

And here is the secret, to catch the Extra, I did it like this in my tabhost class (I read it from here: How to pass a parameter to a new activity with tab host)

下面是tabhost类的主体(Dashboard.java)我的code:

Here is my code in the body of tabhost class (Dashboard.java):

    //passing id to tab #1
    Intent passing1 = new Intent();
    Bundle bundle = getIntent().getExtras();
    String id=bundle.get(ID_USER).toString();   
    passing1.putExtra("iduser", id);
    passing1.setClass(this, Input_form.class);

    TabHost host = getTabHost();
    host.addTab(host.newTabSpec("one").setIndicator("ADD NEW DATA")
            .setContent(passing1));

    //passing idkont to tab #2

    Intent passing2 = new Intent();
    Bundle bundle2=getIntent().getExtras();
    String id2 = bundle2.get(ID_USER).toString();
    passing2.putExtra("iduser", id2);
    passing2.setClass(this, MyData.class);



    host.addTab(host.newTabSpec("two").setIndicator("MY DATA")
            .setContent(passing2));

然后,在活动课两个选项卡的,我只需要赶上额外像往常一样使用getIntent和getStringExtra。
获得额外的两个班后,终于现在我可以用传递给选项卡中的数据/变量在我的应用程序做更多的CRUD。

Then, in the activity class of both tab, I just need to catch the extra like usual using getIntent and getStringExtra. After getting the extra in both class, finally now I can use the data/variable that is passed to the tab to do more CRUD in my app.

希望它能帮助,
谢谢

Hope it helps, Thank you

这篇关于从活动中传递价值的EditText在Android的Tabhost制表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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