英文:
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`.
<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`.
So in `.xml` file `TextView` should have `android:text="@string/no_coupon"`
And in method `onCreateView` you can change text, using method `setText()`.
For example, `couponItem1TextView.setText(R.string.is);`
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论