android处理bar问题

发布于 2024-11-26 09:19:01 字数 1730 浏览 2 评论 0原文

我的程序从服务器检索一些数据。通常需要几秒钟。我想为此生成一个处理栏。我知道如何生成它。但是关闭处理的时候怎么区分和控制呢。 我的程序:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1=(TextView)findViewById(R.id.textin);
        but1=(Button)findViewById(R.id.send);
        but2=(Button)findViewById(R.id.send2);
        edit1=(EditText)findViewById(R.id.textout);

        but1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v) {
                Socket socket=null;
                String mesg=edit1.getText().toString()+"\r\n";
            try {
                socket=new Socket("localhost",30000);
                //send information to server
                PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                out.println(mesg);
                //receive information from server
                BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String mstr=br.readLine();
            if(mstr!=null)
            {
                text1.setText(mstr);
            }else
            {
                text1.setText("error");
            }
                out.close();
                br.close();
                socket.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(Exception e)
            {
                Log.e(DEBUG_TAG,e.toString());
            }
            }        
        });
      }

这是一个客户端程序。数据量确实很大。那么我该如何做处理呢。我怎样才能控制它? 感谢!!!

My programme retrieves some data from server. Usually it takes few seconds. I want to generate a processing bar for this. And I know how to generate it. But how to distinguish and control when close processing bar.
My programme:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1=(TextView)findViewById(R.id.textin);
        but1=(Button)findViewById(R.id.send);
        but2=(Button)findViewById(R.id.send2);
        edit1=(EditText)findViewById(R.id.textout);

        but1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v) {
                Socket socket=null;
                String mesg=edit1.getText().toString()+"\r\n";
            try {
                socket=new Socket("localhost",30000);
                //send information to server
                PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                out.println(mesg);
                //receive information from server
                BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String mstr=br.readLine();
            if(mstr!=null)
            {
                text1.setText(mstr);
            }else
            {
                text1.setText("error");
            }
                out.close();
                br.close();
                socket.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(Exception e)
            {
                Log.e(DEBUG_TAG,e.toString());
            }
            }        
        });
      }

This a client program. The amount of data is really huge. So how can I do the processing bar. How can I CONTROL it?
Thank!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

穿越时光隧道 2024-12-03 09:19:01

您可以为此使用进度对话框。

这是这段代码:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     ProgressDialog mProgressDialog;
      text1=(TextView)findViewById(R.id.textin);
     but1=(Button)findViewById(R.id.send);
     but2=(Button)findViewById(R.id.send2);
     edit1=(EditText)findViewById(R.id.textout);
      but1.setOnClickListener(new Button.OnClickListener()
     {
         public void onClick(View v) {
                public void onClick(View v) {
                mProgressDialog = ProgressDialog.show(Places.this,
            "Please wait...", "Connecting...", true, true, new DialogInterface.OnCancelListener() {public void onCancel(DialogInterface dialog) {
                }});

    new Thread() {
        public void run() {

             Socket socket=null;
             String mesg=edit1.getText().toString()+"\r\n";
         try {
             socket=new Socket("localhost",30000);
             //send information to server
             PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
             out.println(mesg);
             //receive information from server
             BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String mstr=br.readLine();
         if(mstr!=null)
         {
             text1.setText(mstr);
         }else
         { 
            text1.setText("error");
         }
             out.close();
             br.close();
             socket.close();
             sleep(3000);

         } catch (UnknownHostException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }catch(Exception e)
         {
             Log.e(DEBUG_TAG,e.toString());
         }
         mProgressDialog.dismiss();
        }                   

    }.start();
   } 
   }); 
                  } 

You can use Progress Dialog for this.

Here is this code:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     ProgressDialog mProgressDialog;
      text1=(TextView)findViewById(R.id.textin);
     but1=(Button)findViewById(R.id.send);
     but2=(Button)findViewById(R.id.send2);
     edit1=(EditText)findViewById(R.id.textout);
      but1.setOnClickListener(new Button.OnClickListener()
     {
         public void onClick(View v) {
                public void onClick(View v) {
                mProgressDialog = ProgressDialog.show(Places.this,
            "Please wait...", "Connecting...", true, true, new DialogInterface.OnCancelListener() {public void onCancel(DialogInterface dialog) {
                }});

    new Thread() {
        public void run() {

             Socket socket=null;
             String mesg=edit1.getText().toString()+"\r\n";
         try {
             socket=new Socket("localhost",30000);
             //send information to server
             PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
             out.println(mesg);
             //receive information from server
             BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String mstr=br.readLine();
         if(mstr!=null)
         {
             text1.setText(mstr);
         }else
         { 
            text1.setText("error");
         }
             out.close();
             br.close();
             socket.close();
             sleep(3000);

         } catch (UnknownHostException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }catch(Exception e)
         {
             Log.e(DEBUG_TAG,e.toString());
         }
         mProgressDialog.dismiss();
        }                   

    }.start();
   } 
   }); 
                  } 
噩梦成真你也成魔 2024-12-03 09:19:01

如果你只是想告诉用户下载已经完成,那么你可以使用Toast。在下载开始之前抛出“正在下载”消息,并在下载完成时禁用 Toast 消息。

假设您想显示已完成下载的百分比,那么您应该实现自己的进度栏(类似于媒体播放器中的搜索栏)。继续根据完成百分比(例如每秒)更新进度条。

沙什

If you just want to tell user that downloading has been completed, then you can use Toast. Throw the message "Downloading" just before downloading starts and disable Toast message on download completion.

Suppose you want to show the % of completed download, then you should implement your own progress bar (Similar to seek bar in Media Player). Keep on updating progress bar based on % completed (say per second basis).

Shash

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文