多线程管理在视野中?

时间:2012-02-21 13:16:30

标签: android multithreading

我有3个按钮和3个textview。当我按下第一个按钮我希望看到10秒后写入第一个textview.i按第二个按钮我想在5秒后看到在第二个textview中写入.i按第三个按钮,我想看到即时写的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();
            }
        }

2 个答案:

答案 0 :(得分: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> { // }

答案 1 :(得分:0)

如果您只是想在textview中编写一些文本,则不需要线程。改为使用处理程序。

Handler handler = new Handler();

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

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

}