如何以编程方式在android中的xml文件中引用

时间:2022-11-10 10:55:46

I am new to android programming and wanna know this:

我是android编程的新手,想知道这个:

I have 3 strings defined in the strings.xml file: <resources> <string name="one">First Click </string> <string name="two">Second Click </string> <string name="three">Third Click </string> <resources>

我在strings.xml文件中定义了3个字符串: First Click Second Click 第三次单击

and a text view which displays the first string in the strings.xml file. i don't want to use settext("******") to change the text of text view when the user clicks on a button. how can i make the textview switch to the text already defined in the strings.xml file, say from

和一个文本视图,显示strings.xml文件中的第一个字符串。当用户点击按钮时,我不想使用settext(“******”)来更改文本视图的文本。我怎样才能将textview切换到strings.xml文件中已定义的文本,比如说

First Click to Second Click

首先点击进行第二次点击

5 个解决方案

#1


15  

Use setText(getResources().getString(R.string.one));

使用setText(getResources()。getString(R.string.one));

#2


4  

To determine which string you use with a variable you will have to use a switch, as below

要确定使用变量的字符串,必须使用开关,如下所示

switch(anyInt) {
    case 1://if the int == 1, then the textview will be set to this
        tv.setText(getResources().getString(R.string.one);
        break;
    case 2://if the into == 2 then the TV will be set to this
        tv.setText(getResources().getString(R.string.two
        break;
    default:
        tv.setText("into does not have value 1-2")
}

Add as many of these statements as you need, I believe it is very memory efficient even if you have a lot of statements

根据需要添加尽可能多的这些语句,我相信即使你有很多语句,它也非常有效

#3


2  

I Think you need this if i am wrong then get back to me.

如果我错了,我认为你需要这个,然后再回到我身边。

Try this.

尝试这个。

getResources().getString(R.string.app_name);

You just simply read the resource of your application .You can use any resource of your application by getResources() of Resource Class.

您只需阅读应用程序的资源。您可以通过资源类的getResources()使用应用程序的任何资源。

Now , Here you need to read string form String.xml so you can use getString() which is the method of resource so you will get your output Now.

现在,在这里你需要读取字符串形式String.xml,这样你就可以使用getString()这是资源的方法,这样你就可以得到你的输出了。

#4


1  

Get the resources of the application, and then get a string with the ID you are looking for.

获取应用程序的资源,然后获取包含您要查找的ID的字符串。

getResources().getString(R.string.one);

#5


-2  

This would give you the desired effect:

这会给你想要的效果:

yourEditText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {

        String text = yourEditText.getText().toString();
        if(text.equals(getResources().getString(R.string.one)) {

            yourEditText.setText(getResources().getString(R.string.two)
        } else if(text.equals(getResources().getString(R.string.two)) {

            yourEditText.setText(getResources().getString(R.string.three)
        } else  {

            yourEditText.setText(getResources().getString(R.string.one)
        }
    });

#1


15  

Use setText(getResources().getString(R.string.one));

使用setText(getResources()。getString(R.string.one));

#2


4  

To determine which string you use with a variable you will have to use a switch, as below

要确定使用变量的字符串,必须使用开关,如下所示

switch(anyInt) {
    case 1://if the int == 1, then the textview will be set to this
        tv.setText(getResources().getString(R.string.one);
        break;
    case 2://if the into == 2 then the TV will be set to this
        tv.setText(getResources().getString(R.string.two
        break;
    default:
        tv.setText("into does not have value 1-2")
}

Add as many of these statements as you need, I believe it is very memory efficient even if you have a lot of statements

根据需要添加尽可能多的这些语句,我相信即使你有很多语句,它也非常有效

#3


2  

I Think you need this if i am wrong then get back to me.

如果我错了,我认为你需要这个,然后再回到我身边。

Try this.

尝试这个。

getResources().getString(R.string.app_name);

You just simply read the resource of your application .You can use any resource of your application by getResources() of Resource Class.

您只需阅读应用程序的资源。您可以通过资源类的getResources()使用应用程序的任何资源。

Now , Here you need to read string form String.xml so you can use getString() which is the method of resource so you will get your output Now.

现在,在这里你需要读取字符串形式String.xml,这样你就可以使用getString()这是资源的方法,这样你就可以得到你的输出了。

#4


1  

Get the resources of the application, and then get a string with the ID you are looking for.

获取应用程序的资源,然后获取包含您要查找的ID的字符串。

getResources().getString(R.string.one);

#5


-2  

This would give you the desired effect:

这会给你想要的效果:

yourEditText.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {

        String text = yourEditText.getText().toString();
        if(text.equals(getResources().getString(R.string.one)) {

            yourEditText.setText(getResources().getString(R.string.two)
        } else if(text.equals(getResources().getString(R.string.two)) {

            yourEditText.setText(getResources().getString(R.string.three)
        } else  {

            yourEditText.setText(getResources().getString(R.string.one)
        }
    });