简体   繁体   English

如何将 TextView 中的文本设置为字符串 (AndriodStudio)?

[英]How can I set the text in a TextView to a String (AndriodStudio)?

I have seen a lot of responses saying to use getResources().getString(R.string.coupon_1) to get the String from the strings.xml file and change it, but it continues to show the value in the strings.xml file for me instead of showing the String in coupon1, coupon2, or coupon3.我看到很多回复说使用getResources().getString(R.string.coupon_1)strings.xml文件中获取字符串并更改它,但它继续显示strings.xml文件中的值me 而不是在 coupon1、coupon2 或 coupon3 中显示字符串。

This is part of my method with some things I have tried:这是我尝试过的一些方法的一部分:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coupon, container, false);

        couponListTextView = view.findViewById(R.id.couponListTextView);
        couponItem1TextView = view.findViewById(R.id.couponItem1TextView);
        couponItem2TextView = view.findViewById(R.id.couponItem2TextView);
        couponItem3TextView = view.findViewById(R.id.couponItem3TextView);

//        coupon1 = "Is";
//        coupon2 = "this";
//        coupon3 = "working?";

        //coupon1 = getResources().getString(R.string.coupon_1);
        coupon1 = "Is";
        couponItem1TextView.setText(coupon1);
        //coupon2 = getResources().getString(R.string.coupon_2);
        coupon2 = "this";
        couponItem2TextView.setText(coupon2);
        //coupon3 = getResources().getString(R.string.coupon_3);
        coupon3 = "working?";
        couponItem3TextView.setText(coupon3);

        CouponRequest couponRequest = new CouponRequest(coupon1, coupon2, coupon3, responseListener, errorListener);
        RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
        queue.add(couponRequest);
        return inflater.inflate(R.layout.fragment_coupon, container, false);
    }

These are my declarations in strings.xml:这些是我在 strings.xml 中的声明:

<string name="coupon_1">No coupon</string>
<string name="coupon_2">No coupon</string>
<string name="coupon_3">No coupon</string>

These are my TextView declarations in fragment_coupon:这些是我在 fragment_coupon 中的 TextView 声明:

<TextView
            android:id="@+id/couponItem1TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/couponListTextView" />

        <TextView
            android:id="@+id/couponItem2TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="@string/coupon_2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/couponItem1TextView" />

        <TextView
            android:id="@+id/couponItem3TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:text="@string/coupon_3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/couponItem2TextView" />

Any help would be greatly appreciated.任何帮助将不胜感激。

Edit: The strings I am using here are for the purpose of testing the app.编辑:我在这里使用的字符串是为了测试应用程序。 I really need to be able to write using a string whose value is decided at runtime.我真的需要能够使用其值在运行时确定的字符串进行编写。

Edit: I figured it out.编辑:我想通了。 I just needed to return view instead of creating a new instance of View.我只需要返回视图而不是创建一个新的视图实例。

A string is a simple resource that is referenced using the value provided in the name attribute.字符串是使用 name 属性中提供的值引用的简单资源。 So all lines that you use should be in strings.xml .因此,您使用的所有行都应该在strings.xml中。

<string name="no_coupon">No coupon</string>
<string name="is">Is</string>
<string name="this">this</string>
<string name="working">working?</string>

And as I understand it, your TextView are initially set by the string No coupon .据我了解,您的TextView最初由字符串No coupon设置。 So in .xml file TextView should have android:text="@string/no_coupon"所以在.xml文件TextView应该有android:text="@string/no_coupon"

And in method onCreateView you can change text, using method setText() .onCreateView方法中,您可以使用方法setText()更改文本。 For example, couponItem1TextView.setText(R.string.is);例如, couponItem1TextView.setText(R.string.is);

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

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