简体   繁体   English

在xml文件中使用String变量(Android Studio)

[英]Using String variables in xml files (android studio)

So I try to show the String variable in GUI that I inherit from a parent activity. 因此,我尝试在GUI中显示从父活动继承的String变量。

This is my xml code 这是我的xml代码

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:inputType="text"
    android:textStyle="italic"
    android:id="@+id/Edit_Title_Update"
    android:text="@string/updating_title"/>

This is my String definition 这是我的字符串定义

 <string name="updating_title"> %s </string>

This is how I try to put date into the String 这就是我尝试将日期放入字符串的方式

    String updating_title = getString(R.string.updating_title, show_title);
    Log.e("MyTag","Here2 "+updating_title);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_update__diary);

In the console I can see the variable is correct 在控制台中,我可以看到变量是正确的

01-05 10:19:29.767    2849-2849/com.example.wenhaowu.diaryproject E/MyTag﹕ Here2 Happy

But In the emulator the String remains the same 但是在模拟器中,字符串保持不变

在此处输入图片说明

What is the problem and how can I fix it? 有什么问题,我该如何解决? Thank you. 谢谢。

Changing String updating_title won't change the actual String resource updating_title in strings.xml . 更改String updating_title不会改变实际的字符串资源updating_title的strings.xml。

You would need to set text in EditText in your activity after setContentView 您需要在setContentView之后的活动中在EditText中设置文本

Like: 喜欢:

EditText e = (EditText) findViewById (R.id.my_edt_text);
e.setText(updating_title);

Hope this helps. 希望这可以帮助。

nothing is wrong, you just need to find your EditText and call setText on the instance, to update its content with the new value. 没错,您只需要找到您的EditText并在实例上调用setText ,即可使用新值更新其内容。 After setContetView setContetView之后

EditText t = (EditText)  findViewById(R.id.Edit_Title_Update);
t.setText(updating_title);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM