2012-02-21 98 views
2

我有3個按鈕和3個textview.When我按第一個按鈕我想看到10秒後寫在第一個textview.i按第二個按鈕我想看到有些5秒後,這是寫在第二個textview。我按第三個按鈕,我想看看立即寫入的textview。查看多線程管理?

我的問題如何在視圖中工作所有多線程而不鎖定其他視圖?我嘗試了ASCYNTask,但它沒有工作。

有人可以給我任何建議嗎?

我的活動:

package com.example; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MyActivity extends Activity 
{ 
    Button a,b,c; 
    TextView ta,tb,tc; 
    Ascyn ascyn,ascyn2,ascyn3; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     a=(Button)findViewById(R.id.ba); 
     b=(Button)findViewById(R.id.bb); 
     c=(Button)findViewById(R.id.bc); 
     ta=(TextView)findViewById(R.id.ta); 
     tb=(TextView)findViewById(R.id.tb); 
     tc=(TextView)findViewById(R.id.tc); 


     ascyn=new Ascyn(this); 
     ascyn2=new Ascyn(this); 
     ascyn3=new Ascyn(this); 
     a.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       ascyn.execute(); 
       SystemClock.sleep(5000); 
       ta.setText("ok"); 


      } 
     }); 

     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ascyn2.execute(); 
       SystemClock.sleep(1000); 
       tb.setText("ozaman"); 
      } 
     }); 

     c.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ascyn3.execute(); 

       tc.setText("byby");} 
     }); 

    } 
} 





    Ascyn: 





package com.example; 

     import android.app.ProgressDialog; 
     import android.content.Context; 
     import android.os.AsyncTask; 

     /** 
     * Created by IntelliJ IDEA. 
     * User: duygukahraman 
     * Date: 20.02.2012 
     * Time: 15:44 
     * To change this template use File | Settings | File Templates. 
     */ 
     public class Ascyn extends AsyncTask<Void,String,Void> { 
      private Context ctx; 
      ProgressDialog dialog; 

      public Ascyn(Context context){ 
       ctx=context; 
       dialog=new ProgressDialog(ctx); 
      } 

      @Override 
      protected void onPreExecute() { 
      // dialog.setTitle("Please wait"); 
      // dialog.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... unused) { 

       // SystemClock.sleep(20000); 
       return (null); 
      } 

      @Override 
      protected void onPostExecute(Void unused) { 
       dialog.dismiss(); 
      } 
     } 
+0

處理程序。下一個建議:向我們展示您如何使用AsyncTask進行嘗試。 – WarrenFaith 2012-02-21 13:24:39

+0

@ WarrenFaith.I添加了我的代碼。 – DuyguK 2012-02-21 13:30:20

回答

0
You have to create separate Async class for each thread like. 

public class AscynThread1 extends AsyncTask<Void,String,Void> { // } 

public class AscynThread2 extends AsyncTask<Void,String,Void> { // } 

public class AscynThread3 extends AsyncTask<Void,String,Void> { // } 
0

你不需要的線程,如果你只是想編寫一個TextView一些文本。改用處理程序。

Handler handler = new Handler(); 

public void method() { 
    handler.postDelayed(new Runnable() { 

     public void run() { 
      textView.setText("Your text"); 
     } 
    }, 5000); 

} 
+0

什麼是不同的處理程序和線程?我認爲處理程序使用線程? – DuyguK 2012-02-21 14:38:45