我需要全局变量还是 R.string.x 可以?
我脑海中的摘要类似于
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来确实是糟糕的设计。您在做什么,您不知道 x 的类型,并且您需要在应用程序中保留它?
findViewById() 返回的所有内容都是 View 对象或其子类之一。所以,你立刻就知道你有一个视图对象。另一件事是 findViewById 不接受 String 作为参数,它只接受 int 值,这样 R.id.myVar 实际上是与膨胀层次结构中的 View 相对应的整数。
如果你绝对必须做这样的事情,为什么不只使用一个通用的 id 值,以便你总是搜索相同的 id 并将其分配给一个通用的 View 对象,稍后可以使用instanceof进行比较?像这样:
在这种情况下,您的 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:
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
R.string.
是 string.xml 文件中定义的字符串的 ID。该行将失败,因为
findViewById
的参数是一个 id,您可以在R.id.
中找到它。如果您说要在 string.xml 文件中定义名称和年龄,那么您可以从代码中的任何
Activity
或View
进行访问。我怀疑您真正想要的是拥有一个可设置且可从多个视图访问的名称和年龄变量。要将数据从一个
Activity
传递到另一个Activity
,您可以将它们放入Intent
中的 extras 中。例如,在启动 Activity2.java 的 Activity1.java 中,您可以执行以下操作:
然后,在 Activity2.java 中,您可以获得放入 Intent 中的额外内容,例如:
R.string.<anything>
are ids of strings defined in your string.xml file. The linewill fail because the parameter to
findViewById
is an id, which you can find inR.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
orView
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 anotherActivity
, you can put them into the extras in theIntent
.For example, in Activity1.java where you start Activity2.java, you could do something like:
Then, in Activity2.java, you can get the extras you put into the Intent, something like: