如何将TextView中的文本设置为一个字符串(Andriod Studio)?

huangapple go评论53阅读模式
英文:

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.

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:

<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:

<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.
英文:

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.

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:

<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:

<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.

答案1

得分: 1

<string name="no_coupon">没有优惠券</string>
<string name="is"></string>
<string name="this">这是</string>
<string name="working">有效吗?</string>
<TextView
    android:text="@string/no_coupon" />

onCreateView 方法中,您可以使用 setText() 方法更改文本。
例如,couponItem1TextView.setText(R.string.is);


<details>
<summary>英文:</summary>
A string is a simple resource that is referenced using the value provided in the name attribute.
So all lines that you use should be in `strings.xml`.
&lt;string name=&quot;no_coupon&quot;&gt;No coupon&lt;/string&gt;
&lt;string name=&quot;is&quot;&gt;Is&lt;/string&gt;
&lt;string name=&quot;this&quot;&gt;this&lt;/string&gt;
&lt;string name=&quot;working&quot;&gt;working?&lt;/string&gt;
And as I understand it, your `TextView` are initially set by the string `No coupon`.
So in `.xml` file `TextView` should have `android:text=&quot;@string/no_coupon&quot;`
And in method `onCreateView` you can change text, using method `setText()`.
For example, `couponItem1TextView.setText(R.string.is);`
</details>

huangapple
  • 本文由 发表于 2020年5月3日 21:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/61574934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定