• Android 更新UI的两个方法


    Android 更新UI的两个方法

    在Android的开发过程中,常常需要适时的更新UI。Androd中的UI是在主线程中更新的。如果在主线程之外的线程中直接更新,就会出现报错并抛出异常:

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

    只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)

    那么Android中该如何更新UI呢?

    <1>. 利用Activity.runOnUiThread(Runnable)把更新UI的代码写在Runnable中

          操作机制:如果当前线程是UI线程,那么该行动立即执行;如果不是,操作发布到事件队列的UI线程。


    RunOnUiThreadDemo


    布局文件:activity_mai.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    	    <TextView
    	        android:layout_width="match_parent"
    	        android:layout_height="wrap_content"
    	        android:text="@string/hello_world" 
    	        />
    	    <Button 
    	        android:id="@+id/btn"
    	        android:layout_width="match_parent"
    	        android:layout_height="wrap_content"
    	        android:text="点击更新界面"
    	        />
    	    <TextView
    	        android:id="@+id/input_str"
    	        android:layout_width="wrap_content"
    	        android:layout_height="wrap_content"
    	        />
    
    
    </LinearLayout>
    


    Java文件:MainActivity.java

    package com.zsl.runonuithreaddemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.text.Editable;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		final TextView viewText = (TextView)findViewById(R.id.input_str);
    		Button btn = (Button)findViewById(R.id.btn);
    		btn.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View arg0) {
    				// TODO Auto-generated method stub
    				MainActivity.this.runOnUiThread(new Runnable() {
    
    					@Override
    					public void run() {
    						// TODO Auto-generated method stub
    						char[] str = "Android is intresting...".toCharArray();
    						viewText.setText(str, 0, str.length);
    					}
    				});
    				
    			}
    		});
    	}
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    


    运行结果:

    点击Button之前:                                       点击Button之后:

                  


    <2>. 在Activity.onCreate(Bundle savedInstanceState)中创建一个Handler类的实例,利用Handler的回调实现。

          具体参见:Android Handler消息传递机制

    事实上,我从这里的一篇英文文章中了解到,Android的handler中也是通过runOnUiThread实现的。


  • 相关阅读:
    光盘和U盘
    解决时间同步
    僵尸进程 和 孤儿进程
    Centos虚拟机设置网络模式
    常用CDN 和 后台管理模板
    微信小程序wxs如何使用
    kubernetes/client-go--使用 Clientset 获取 Kubernetes 资源对象
    samplecontroller
    volcano
    DNS欺骗
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3177626.html
Copyright © 2020-2023  润新知