gpt4 book ai didi

android - 如何将额外的 Intent 传递给两个 Activity

转载 作者:行者123 更新时间:2023-11-29 16:04:03 25 4
gpt4 key购买 nike

我有一个应用程序,在第一个 Activity 中询问第二页上的人名,它在句子中显示姓名我想在第三个第四个或第九个 Activity 中使用我如何正确声明它(公开?)并在我需要的时间和地点调用它?这是我发送的代码

主要

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);

}
});

}

这是接收它的 Activity 2

@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);

请问我该如何重用它?

最佳答案

使用 SharedPreferences 保存名称或您需要的任何变量,然后在需要时读取。首先,在 MainActivity 中创建 global,它将用作首选项文件名:

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");

保存后,每个 Activity 都可以访问首选项。

因此,在您的情况下,按下确定按钮时保存名称:

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);

您可以在每项需要的 Activity 中进行类似的操作。请参阅文档 here .

关于android - 如何将额外的 Intent 传递给两个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21055702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com