我如何将数字/字符串存储到 R.string.xx 中?

发布于 2024-12-27 09:01:00 字数 963 浏览 1 评论 0原文

使用这段代码,我的程序只是强制关闭(错误),

***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 技术交流群。

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

发布评论

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

评论(4

塔塔猫 2025-01-03 09:01:00

我不确定您是否可以将文本保存到 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.

梦旅人picnic 2025-01-03 09:01:00

至少在变量 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.

不醒的梦 2025-01-03 09:01:00

您不能将值设置为 R.string.xxx,因为所有这些值都将是常量,就像只读的东西一样。如果您想将编辑文本的值用于另一个布局,请使用类变量或 intent.putextra()

来到您的源代码,我看到这个

public View x = findViewById(R.string.nfoname);

How can a view be find by R.String?这应该是 R.id。

最终 EditText infoname=(EditText)findViewById(R.id.infoname);
为什么这个 editText 必须是最终的?

***x=(View) infoname.getText();***

您只需使用 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

public View x = findViewById(R.string.nfoname);

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?

***x=(View) infoname.getText();***

You just use infoname.getText().toString() you will get the string value of the Edittext's current text.
Dude you can do stuff simply.

用心笑 2025-01-03 09:01:00
public View x = findViewById(R.string.nfoname);

这不起作用,因为您不仅尝试使用 R.string 资源 id 查找 View,而且还在 setContenView(... ) 在您的 onCreate(...) 方法中调用。即使您使用了有效的 View 资源 ID(例如 R.id.infoname),x 也将为 null,因为内容视图尚未膨胀。

final EditText infoname=(EditText)findViewById(R.id.infoname);

除了毫无意义地使用 final 之外,只要 R.id.infoname 实际上是 EditText 的资源 ID,这就不应该引起问题。 。

x=(View) infoname.getText();

x 不仅会是 null,而且在 EditText 上调用 getText() 会返回一个 Editable 不是 View,也不可能将其转换为 View。即使您使用了 getText().toString() 这是从 EditText 获取文本的正确方法,它仍然无法转换 字符串视图

还有,至于这个……

TextView x = setText(R.string.nfoname);

一定是……

TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));
public View x = findViewById(R.string.nfoname);

This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.

final EditText infoname=(EditText)findViewById(R.id.infoname);

Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.

x=(View) infoname.getText();

Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.

Also, as for this...

TextView x = setText(R.string.nfoname);

It would have to be...

TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文