Android TextView即使在调试时也不会更新

时间:2014-07-16 12:45:38

标签: android android-activity xamarin.ios xamarin textview

我有一个非常基本的布局代码的活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id = "@+id/WebServiceClassInformation"
        android:text = "Making connection with servers"
        android:layout_centerInParent = "true"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content">
    </TextView>

</RelativeLayout>

在我的OnCreate()方法中,我正在更新此文本视图的值。其代码如下:

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView (Resource.Layout.CallWebServiceLayout);

    webServiceClassInfo = FindViewById<TextView> (Resource.Id.WebServiceClassInformation);

    if (InternetConnectionCheck._InternetIsAvailable (this))
    {
        webServiceClassInfo.Text = "Stay Connected to Internet";
        System.Threading.Thread.Sleep (1000);
    }

    StartService (new Intent(this, typeof(MyService)));

    StartActivity (new Intent(this, typeof(Login)));
}

现在,这是一件有趣的事情。在调试时,当控件处于“保持连接”状态,并将其传递到睡眠线时,TextView不显示在我的手机屏幕上,更新的文本也没有显示,但它会休眠1秒,然后导航到登录活性。

从那里开始,当我按下时,我可以看到TextView的更新值。这是手机截图。

enter image description here

所以这就是问题所在。我希望用户看到文本1秒,但它导航到下一个活动。有什么建议 ??我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

您应该覆盖OnResume并将代码放在那里。制作resum async函数,然后使用await。这将允许UI自行更新。我没有测试下面的样本,但是应该做这个工作。

    protected async override void OnResume()
    {
        base.OnResume();

        if (InternetConnectionCheck._InternetIsAvailable (this))
        {
            webServiceClassInfo = FindViewById<TextView> (Resource.Id.WebServiceClassInformation);
            webServiceClassInfo.Text = "Stay Connected to Internet";
            await Task.Delay(1000);
        }

        StartService (new Intent(this, typeof(MyService)));

        StartActivity (new Intent(this, typeof(Login)));
    }

答案 1 :(得分:0)

使用postDelayed(Runnable r,long delayMillis)开始您的活动和服务。

Runnable r = new Runnable() {
        @Override
        public void run() {
            StartService (new Intent(this, typeof(MyService)));
            StartActivity (new Intent(this, typeof(Login)));
        }
    };
new Handler().postDelayed(r, 1000);

当你使用Thread.sleep(1000)时,1秒内没有任何反应是正常的......