Xamarin Android Fragment TextView SetText

时间:2015-11-02 18:00:13

标签: android android-fragments xamarin xamarin.android

我创建了一个Fragement,并在片段中放置了TextView。我想更新TextView的Text。我使用了以下代码,但它无效。

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        ViewGroup root = (ViewGroup)inflater.Inflate(Resource.Layout.homefragmentlayout, null);

        var fragmentView = inflater.Inflate(Resource.Layout.homefragmentlayout, null);

        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Interval = 1000; 
        timer.Elapsed += OnTimedEvent;
        timer.Enabled = true;
        timer.Start ();

        DateTime oldDate = new DateTime(2015, 12, 25);
        DateTime newDate = DateTime.Now;
        TimeSpan timeSpan = oldDate - newDate;

        var content = new SpannableString (timeSpan.Seconds.ToString("00"));
        TextView txtSeconds = fragmentView.FindViewById<TextView>(Resource.Id.txtSeconds);
        txtSeconds.SetText(content, TextView.BufferType.Spannable);

        return root;
    }

如果我错过了,请告诉我。

1 个答案:

答案 0 :(得分:0)

使用从INCLUDING = -Isrc -Iheaders 方法返回的相同View对象来访问当前Fragment Layout中的View。

目前正在从方法返回For Each newSheetName In dataRange If newSheetName.Value <> "" Then Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet Sheets(Sheets.Count).Name = newSheetName.Value ' renames the new worksheet End If Next newSheetName ,但使用OnCreateView访问和更新root中的文字。

所以只需将return语句更改为:

fragmentView