我如何将数字/字符串存储到 R.string.xx 中?
使用这段代码,我的程序只是强制关闭(错误),
***public View x = findViewById(R.string.nfoname);***
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
带有 * 的程序是错误
程序函数的来源: 如果用户点击确认,他的名字将被设置为 R.string.nfoname 然后将通过 TextView x = setText(R.string.nfoname); 在另一个布局中使用它
with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function:
if the user clicks confirm, his name will be set to R.string.nfoname
which will then be used in another layout through TextView x = setText(R.string.nfoname);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您是否可以将文本保存到 R.string。这是编译器为您创建的生成类。它与您的 apk 一起打包。将资源视为翻译并将文本呈现到屏幕上的手段。
我认为您想要做的是将用户输入保存为 SharedPreference 或数据库中。
有关示例用法,请参阅 Android 文档上的SharedPreferences 。
I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.
至少在变量 infoname 范围的情况下最有可能导致您的应用程序抛出错误。 infoname 是函数 onCreate() 的局部变量,而不是类的实例变量,因此 onClick() 方法无法访问它,因为它们是匿名类的一部分。
我要问的另一件事是你为什么将 infoname 标记为最终的?当 onCreate() 退出时,它会超出范围,因此如果它发生更改,您可以看到谁更改了它,因为它仅在方法执行时存在。
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
您不能将值设置为 R.string.xxx,因为所有这些值都将是常量,就像只读的东西一样。如果您想将编辑文本的值用于另一个布局,请使用类变量或
intent.putextra()
来到您的源代码,我看到这个
How can a view be find by R.String?这应该是 R.id。
最终 EditText infoname=(EditText)findViewById(R.id.infoname);
为什么这个 editText 必须是最终的?
您只需使用 infoname.getText().toString() 即可获得 Edittext 当前文本的字符串值。
伙计,你可以简单地做事。
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or
intent.putextra()
Coming to ur source code i see this
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Why this editText has to be final?
You just use
infoname.getText().toString()
you will get the string value of the Edittext's current text.Dude you can do stuff simply.
这不起作用,因为您不仅尝试使用
R.string
资源 id 查找View
,而且还在setContenView(... )
在您的onCreate(...)
方法中调用。即使您使用了有效的View
资源 ID(例如R.id.infoname
),x
也将为null
,因为内容视图尚未膨胀。除了毫无意义地使用
final
之外,只要R.id.infoname
实际上是EditText
的资源 ID,这就不应该引起问题。 。x
不仅会是null
,而且在EditText
上调用getText()
会返回一个Editable 不是
View
,也不可能将其转换为View
。即使您使用了getText().toString()
这是从EditText
获取文本的正确方法,它仍然无法转换字符串
到视图
。还有,至于这个……
一定是……
This can't work as not only are you trying to find a
View
using aR.string
resource id, you are doing it beforesetContenView(...)
is called in youronCreate(...)
method. Even if you used a validView
resource id such asR.id.infoname
thenx
will benull
because the content view hasn't been inflated yet.Apart from the pointless use of
final
this should'nt cause problems as long asR.id.infoname
is actually the resource id of anEditText
.Not only will
x
benull
but callinggetText()
on anEditText
returns anEditable
which is not aView
nor is it possible to cast it toView
. Even if you usedgetText().toString()
which is the correct way to get the text from anEditText
it still wouldn't be possible to cast aString
to aView
.Also, as for this...
It would have to be...