Android??????UI????????
???????????? ???????[ 2015/9/6 11:04:27 ] ??????????????? ???????
??????????????????????android????????UI????и???UI???????????????и???UI????????????4??????????????????
???????Handler???????????
???????AsyncTask??????
???????runOnUiThread(action)??????
???????Handler??post(Runnabel r)??????
????????????????????????????TextView??
????1.???Handler??????????
????package com.example.runonuithreadtest;
????import android.app.Activity;
????import android.os.Bundle;
????import android.os.Handler;
????import android.widget.TextView;
????public class MainActivity extends Activity {
???? private TextView tv;
???? Handler handler = new Handler()
???? {
???? public void handleMessage(android.os.Message msg) {
???? if(msg.what==0x123)
???? {
???? tv.setText("???o??TextView");
???? }
???? };
???? };
???? @Override
???? protected void onCreate(Bundle savedInstanceState) {
???? super.onCreate(savedInstanceState);
???? setContentView(R.layout.activity_main);
???? tv = (TextView) findViewById(R.id.tv);
???? new MyThread().start();
???? }
???? class MyThread extends Thread
???? {
???? @Override
???? public void run() {
???? //??????????
???? try {
???? Thread.sleep(2000);
???? } catch (InterruptedException e) {
???? // TODO Auto-generated catch block
???? e.printStackTrace();
???? }
???? handler.sendEmptyMessage(0x123);
???? }
???? }
????}
????2. ???AsyncTask??????
???????????UI??????????onPostExecute(String result)?????С?
????package com.example.runonuithreadtest;
????import android.app.Activity;
????import android.os.AsyncTask;
????import android.os.Bundle;
????import android.widget.TextView;
????public class MainActivity extends Activity {
???? private TextView tv;
???? @Override
???? protected void onCreate(Bundle savedInstanceState) {
???? super.onCreate(savedInstanceState);
???? setContentView(R.layout.activity_main);
???? tv = (TextView) findViewById(R.id.tv);
???? new Yibu().execute();
???? }
???? class Yibu extends AsyncTask<String?? String?? String>
???? {
???? @Override
???? protected String doInBackground(String... params) {
???? try {
???? Thread.sleep(2000);
???? } catch (InterruptedException e) {
???? // TODO Auto-generated catch block
???? e.printStackTrace();
???? }
???? return null;
???? }
???? @Override
???? protected void onPostExecute(String result) {
???? // TODO Auto-generated method stub
???? tv.setText("???o??TextView");
???? }
???? }
????}
????3. ???runOnUiThread(action)????
????package com.example.runonuithreadtest;
????import android.app.Activity;
????import android.os.Bundle;
????import android.widget.TextView;
????public class MainActivity extends Activity {
???? private TextView tv;
???? @Override
???? protected void onCreate(Bundle savedInstanceState) {
???? super.onCreate(savedInstanceState);
???? setContentView(R.layout.activity_main);
???? tv = (TextView) findViewById(R.id.tv);
???? new MyThread().start();
???? }
???? class MyThread extends Thread
???? {
???? @Override
???? public void run() {
???? runOnUiThread(new Runnable() {
???? @Override
???? public void run() {
???? // TODO Auto-generated method stub
???? try {
???? //??????????
???? Thread.sleep(2000);
???? } catch (InterruptedException e) {
???? e.printStackTrace();
???? }
???? tv.setText("???o??TextView");
???? }
???? });
???? }
???? }
????}
????4. ???Handler??post(Runnabel r)????
????package com.example.runonuithreadtest;
????import android.app.Activity;
????import android.os.Bundle;
????import android.os.Handler;
????import android.widget.TextView;
????public class MainActivity extends Activity {
???? private TextView tv;
???? @Override
???? protected void onCreate(Bundle savedInstanceState) {
???? super.onCreate(savedInstanceState);
???? setContentView(R.layout.activity_main);
???? tv = (TextView) findViewById(R.id.tv);
???? Handler handler = new Handler();
???? handler.post(new Runnable(){
???? @Override
???? public void run() {
???? try {
???? //??????????
???? Thread.sleep(2000);
???? } catch (InterruptedException e) {
???? e.printStackTrace();
???? }
???? tv.setText("???o??TextView");
???? }
???? });
???? }
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11