指定Toast显示时长(通过线程)

public class MainActivity extends Activity {  
  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
  
        Button button = (Button) findViewById(R.id.button);  
        button.setOnClickListener(new OnClickListener() {  
  
  
            @Override  
            public void onClick(View v) {  
                Toast toast = Toast.makeText(MainActivity.this, "abcdefg",  
                        Toast.LENGTH_LONG);  
                showToast(toast, 10); // 显示10秒  
            }  
        });  
    }  
  
  
    /** 
     *  
     * @param toast 
     * @param second 
     */  
    private void showToast(final Toast toast, final int second) {  
        new Thread(new Runnable() {  
            int tempSecond = 0;  
  
            @Override  
            public void run() {  
                while (tempSecond < second) {  
                    toast.show();  
                    try {  
                        Thread.sleep(1000);  
                    } catch (InterruptedException e) {  
                    }  
                    tempSecond++;  
                }  
                toast.cancel();  
            }  
        }).start();  
    }  

转: http://blog.csdn.net/tangcheng_ok

posted @ 2013-05-01 01:17  编程小爬虫  阅读(480)  评论(0编辑  收藏  举报