![](/img/trans.png)
[英]How to add OnclickListener() for a Button within a Inflated View?
[英]How to set text of a button contained within a dynamically inflated view?
我正在嘗試動態設置在運行時膨脹的按鈕的文本,但未設置文本。
我創建一個自定義對話框為我設定SetContentView
一個View
,我在運行時膨脹。 該視圖包含一個Button
,例如:
var dialog = new SetUnsetDialog();
var view = View.Inflate(_context, Resource.Layout.view_set_unset_buttons, null);
view.FindViewById<Button>(Resource.Id.btnPartsetA).Text = _blockDescriptions["partseta"];
dialog.SetContentView(view);
dialog.SetTitle("Set/Unset " + _area);
dialog.Show();
布局定義為:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- irrelevant stuff removed -->
<LinearLayout
android:id="@+id/linearLayoutSetUnsetButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<!-- irrelevant stuff removed -->
<Button
android:id="@+id/btnPartsetA"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:drawableTop="@drawable/partset_a" />
<!-- irrelevant stuff removed -->
</LinearLayout>
</LinearLayout>
為什么未設置按鈕文字? 我需要做什么設置?
更新:
事實證明這不是問題。 內容視圖稍后會重置,並覆蓋我設置的值。
使用setText
方法而不是.Text
怎么樣?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.