我需要全局变量还是 R.string.x 可以?

发布于 2024-12-27 19:58:46 字数 520 浏览 2 评论 0原文

我脑海中的摘要类似于

idontknow x = (idontknow) findViewById(R.string.stringname);

这样,每当我更改 x 时,R.string.stringname 也会更改

,因此我可以在同一项目中的任何位置使用 R.string.stringname

,但显然这是一个错误

编辑: 我想在 formActivity 的 EditText 字段上输入我的名字(R.string.name 或 global)和年龄(R.string.age 或 global)

,并在另一个不同的 Activity 中输入我想要的:

TextView x = (TextView) findViewById(<some id>);
x.setText("Hello "+ getString(R.string.nfoname)+ "you are"+getString(R.string.nfoname)+"years old");

an abstract in my mind is something like

idontknow x = (idontknow) findViewById(R.string.stringname);

so that whenever i change x the R.string.stringname would also change

and thus i can use the R.string.stringname anywhere in the same project

but obviously it is an error

edit:
i would like to input my name(R.string.name or global) and age(R.string.age or global) on an EditText field on a formActivity

and in another differentActivity i would like to have this:

TextView x = (TextView) findViewById(<some id>);
x.setText("Hello "+ getString(R.string.nfoname)+ "you are"+getString(R.string.nfoname)+"years old");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演出会有结束 2025-01-03 19:58:46

这看起来确实是糟糕的设计。您在做什么,您不知道 x 的类型,并且您需要在应用程序中保留它?

findViewById() 返回的所有内容都是 View 对象或其子类之一。所以,你立刻就知道你有一个视图对象。另一件事是 findViewById 不接受 String 作为参数,它只接受 int 值,这样 R.id.myVar 实际上是与膨胀层次结构中的 View 相对应的整数。

如果你绝对必须做这样的事情,为什么不只使用一个通用的 id 值,以便你总是搜索相同的 id 并将其分配给一个通用的 View 对象,稍后可以使用instanceof进行比较?像这样:

View view = findViewById(R.id.generic_id);
if(view instanceof TextView) {TextView actualView=(TextView)findViewById(R.id.generic_id);}
else {ImageView actualView = (ImageView)findViewById(R.id.generic_id);}
return actualView;

在这种情况下,您的 ID 可能是一个无法更改的常量,并且您始终可以弄清楚您正在处理的 View 的子类。在我看来,这感觉像是一个比全局变量浮动更易于维护的设计

This really seems like poor design. What are you doing that you don't know the type of x, and it's something that you need to persist across your application?

Everything that gets returned by findViewById() is a View object or one of its subclasses. So, right off the bat you know you have a view object. Another thing is that findViewById doesn't accept a String as a parameter, it only accept an int value, such that R.id.myVar is actually an integer corresponding to a View in the inflated hierarchy.

If you absolutely have to do something like that, why wouldn't you just use a common id value so that you're always searching for the same id and assign it to a generic View object that can be compared using instanceof later? Like this:

View view = findViewById(R.id.generic_id);
if(view instanceof TextView) {TextView actualView=(TextView)findViewById(R.id.generic_id);}
else {ImageView actualView = (ImageView)findViewById(R.id.generic_id);}
return actualView;

In that case, your ID could be a constant that couldn't be changed, and you always can figure out what subclass of View you're dealing with. This feels like a more maintainable design than having global variables floating around, in my opinion

云巢 2025-01-03 19:58:46

R.string. 是 string.xml 文件中定义的字符串的 ID。该行将

idontknow x = (idontknow) findViewById(R.string.stringname);

失败,因为 findViewById 的参数是一个 id,您可以在 R.id. 中找到它。

如果您说要在 string.xml 文件中定义名称和年龄,那么您可以从代码中的任何 ActivityView 进行访问。

我怀疑您真正想要的是拥有一个可设置且可从多个视图访问的名称和年龄变量。要将数据从一个 Activity 传递到另一个 Activity,您可以将它们放入 Intent 中的 extras 中。

例如,在启动 Activity2.java 的 Activity1.java 中,您可以执行以下操作:

Intent i = new Intent(this, Activity2.class);
i.putExtra("Name", name); // name is a variable with the name value you want to send
i.putExtra("Age", age); // age is a variable with the age value you want to send
startActivity(i);

然后,在 Activity2.java 中,您可以获得放入 Intent 中的额外内容,例如:

Bundle extras = getIntent().getExtras();
name = extras.getString("Name");
age = extras.getInt("Age");

R.string.<anything> are ids of strings defined in your string.xml file. The line

idontknow x = (idontknow) findViewById(R.string.stringname);

will fail because the parameter to findViewById is an id, which you can find in R.id.<anthing>.

If you are saying you want to define the name and age in the string.xml file, then you do have access from any Activity or View in your code.

What I suspect you actually want is to have a name and age variable that is settable and accessible from multiple views. To pass data from one Activity to another Activity, you can put them into the extras in the Intent.

For example, in Activity1.java where you start Activity2.java, you could do something like:

Intent i = new Intent(this, Activity2.class);
i.putExtra("Name", name); // name is a variable with the name value you want to send
i.putExtra("Age", age); // age is a variable with the age value you want to send
startActivity(i);

Then, in Activity2.java, you can get the extras you put into the Intent, something like:

Bundle extras = getIntent().getExtras();
name = extras.getString("Name");
age = extras.getInt("Age");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文