如何通过额外的意图两项活动 [英] how to pass extra intent to two activities

查看:183
本文介绍了如何通过额外的意图两项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序上的第一个活动要求它显示在一个句子我想用这个名字在第三第四或第9个活动命名为第二页上的人的名字我如何正确地将其声明(公开?)并调用它何时何地,我需要它?这是我的code发送它

i have an app that on the first activity asks the persons name on the second page it displays the name in a sentence i want to use the name in the third fourth or 9th activity how do i properly declare it (public?) and call it when and where ever i need it? this is my code sending it

主要

public class MainActivity extends Activity {
Button ok;
EditText name;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(EditText)findViewById(R.id.editText);
    Typeface font_a = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
    name.setTypeface(font_a);
    ok=(Button)findViewById(R.id.button);
       Typefacefont_b=Typeface.createFromAsset(getAssets(),
"fonts/SignPaintersGothicShaded.ttf");
    ok.setTypeface(font_b);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            String nameStr = name.getText().toString();

            Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
            intent.putExtra("NAMEDATA",nameStr);
            startActivity(intent);

        }
    });

}

这是活动2接收它

and this is activity 2 receiving it

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    t = (TextView)findViewById(R.id.textView3);
    Typeface font_b = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
    t.setTypeface(font_b);
    String n = this.getIntent().getStringExtra("NAMEDATA");
    t.setText(n);

所以请我将如何重用呢?

so please how would i reuse this?

推荐答案

使用共享preferences,以节省您需要的名称或任何变量,然后读取只要你需要它。首先,在MainActivity创建全局哪个将用作preference文件名:

Use SharedPreferences to save the name or whatever variable you need, then read whenever you need it. First, create global in MainActivity which will be used as preference file name:

public static final String PREFS_NAME = "MyPrefsFile";

然后,保存:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("name", name);
editor.commit();

加载:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
String name = settings.getString("name", "John");

一旦保存,preFS对每一项活动进行访问。

Once saved, the prefs are accessible for every activity.

因此​​,在你的情况下,保存名称时确定按钮是pressed:

So in your case, save the name when ok button is pressed:

ok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        String nameStr = name.getText().toString();

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("name", nameStr);
        editor.commit();

        Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(intent);

    }
});

然后阅读:

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

    t = (TextView)findViewById(R.id.textView3);
    Typeface font_b = Typeface.createFromAsset(getAssets(),"fonts/MarkerFelt.ttf");
    t.setTypeface(font_b);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    String n = settings.getString("name", "defaultName");
    t.setText(n);

您可以在每一个需要它的活动同样做。见文档这里

You can do similarly in every activity you need it. See docs here.

这篇关于如何通过额外的意图两项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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