使得Android应用程序时,XML文档中我可以直接编辑字符串资源? [英] Can I directly edit string resources in an xml document when making android apps?

查看:121
本文介绍了使得Android应用程序时,XML文档中我可以直接编辑字符串资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习Android的节目。

Android开发教程

和我已经创建了一个启动一个新的活动的应用程序。

Anywho,这将启动和所有。
`
   公共最后静态字符串EXTRA_MESSAGE =com.example.myfirstapp.MESSAGE;
    公共静态INT counterNumber = 0;

 公共无效thishappens(查看视图){
    意向意图=新意图(这一点,DisplayMessage.class);
    TextView中的TextView =(的TextView)findViewById(R.id.edit_message);
    字符串消息= textView.getText()的toString()。
    intent.putExtra(EXTRA_MESSAGE,消息);
    startActivity(意向);`

上面推出新的活动displayMessage.class,一个片段是楼下。

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_display_message);
    意向意图= getIntent();
    字符串消息= intent.getStringExtra(MyActivity.EXTRA_MESSAGE);    //创建文本视图
    TextView中的TextView =新的TextView(本);
    textView.setTextSize(40);
    textView.setText(消息);    //布局刷新
    的setContentView(TextView的);
    的setContentView(R.layout.activity_display_message);
}

我的问题是,底部两行。我既可以载入我的布局或code创建的文本视图。

所以我的问题是,我可以直接从XML文件中编辑字符串资源?

我想有我并直接加载到activity_display_message布局的字符串。

林不知道怎么样。

我如何有一个用户输入输入,有它在转移到下一个活动,同时仍保持布局我设计该活动?


解决方案

  

所以我的问题是,我可以直接从XML文件中编辑字符串资源?


没有,你不能改变资源在运行时。但是你的code似乎有点愚蠢。这一切会在活动启动运行。所以,你可以检查邮件,然后只设置在你的布局的TextView

 保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_display_message);
    TextView中的TextView(的TextView)findViewById(R.id.textView);
    意向意图= getIntent();
    字符串消息= intent.getStringExtra(MyActivity.EXTRA_MESSAGE);    textView.setText(消息);
}

只需创建一个的TextView 里面的 activity_display_message.xml (这里叫做的TextView ),然后用消息文本变量。

此外,它往往是一个好主意,做一些错误检查,如

 如果(意向== NULL和放大器;!&安培;!intent.getStringExtra(MyActivity.EXTRA_MESSAGE = NULL)
{
    //做你的东西在这里
}

不过还是你想要去的这一点。

存储...

您可以在一个持续的值,值存储,如共享preferences ,文件,数据库等......如果你是想以后用它

您可以阅读更多有关在这里的文档

I'm trying to learn android programming.

Following the tutorial at Android Dev Tutorial

And I've created an app that launches a new activity.

Anywho, this launches and all that. ` public final static String EXTRA_MESSAGE ="com.example.myfirstapp.MESSAGE"; public static int counterNumber=0;

public void thishappens(View view){
    Intent intent = new Intent(this, DisplayMessage.class);
    TextView textView = (TextView) findViewById(R.id.edit_message);
    String message = textView.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);`

the above launches the new activity displayMessage.class, a snippet is down below.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    //Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    //layout refresh
    setContentView(textView);
    setContentView(R.layout.activity_display_message);
}

My problem is with the bottom two lines. I can either load my layout or the text view created in the code.

so my question is, can I directly edit the string resources from the xml file?

I'd like to have the string that i did load directly into the activity_display_message layout.

Im not sure how.

How do I have a user enter input and have it transfer over to the next activity, while still keeping the layout i designed for that activity?

解决方案

so my question is, can I directly edit the string resources from the xml file?

No, you can't change resources at runtime. But your code seems kind of goofy. This is all going to run when the Activity starts up. So, you can check the message then just set it in your TextView of the layout.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    TextView textView (textView) findViewById(R.id.textView);
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

    textView.setText(message);
}

simply create a TextView inside of activity_display_message.xml (here called textView) then set the text with the message variable.

Also, it is often a good idea to do some error checking such as

if (intent !== null && intent.getStringExtra(MyActivity.EXTRA_MESSAGE != null)
{
    // do your stuff here
}

or however you want to go about that.

Storing...

You could store that value in a persistent value such as SharedPreferences, a file, db, etc... if you are wanting to use it later.

You can read more about that here in the docs

这篇关于使得Android应用程序时,XML文档中我可以直接编辑字符串资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆