程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-怎麼一打開app,就出現取消關注中,程序也無法退出

編程-怎麼一打開app,就出現取消關注中,程序也無法退出

編輯:編程解疑
怎麼一打開app,就出現取消關注中,程序也無法退出

怎麼一打開app,就出現取消關注中,程序也無法退出,應該先出現關注中啊
圖片說明

 package com.example.ch02_progressdialogdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class ProgressDialogActivity extends Activity implements OnClickListener{
    private Button button=null;
    public ProgressDialog dialog=null;
    int flag;                                       
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_dialog);
        button=(Button)findViewById(R.id.bt1);
        button.setEnabled(true);
        button.setText("加關注");
        flag=0;
        button.setOnClickListener(this);
        dialog=ProgressDialog.show(ProgressDialogActivity.this,"","取消關注中...",true);
        mHandler.sendEmptyMessageDelayed(0,3000);
    }

    Handler mHandler=new Handler()
    {
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
        button.setEnabled(false);
        button.setText("加關注");
        Toast.makeText(getBaseContext(), "取消關注成功", Toast.LENGTH_SHORT).show();
    }
    };

    @Override
    public void onClick(View v)
    {
       switch (v.getId())
       {
       case R.id.bt1:
              if (flag==0)
              {
              dialog=ProgressDialog.show(ProgressDialogActivity.this,"","關注中...",true);
              new Thread()
              {
                  public void run()
                  {
                      try
                      {
                          sleep(3000);
                      }
                      catch(InterruptedException e)
                      {
                          e.printStackTrace();
                      }
                      finally
                      {
                          dialog.dismiss();
                      }
                  }
              }.start();
              button.setEnabled(false);
              button.setText("已關注");
              Toast.makeText(getBaseContext(), "關注成功", Toast.LENGTH_SHORT).show();
              flag=1;
              }
              else if (flag==1)
              {
                  dialog=ProgressDialog.show(ProgressDialogActivity.this,"","取消關注中...",true);
                  new Thread()
                  {
                      public void run()
                      {
                          try
                          {
                              sleep(3000);
                          }
                          catch(InterruptedException e)
                          {
                              e.printStackTrace();
                          }
                          finally
                          {
                              dialog.dismiss();
                          }
                         // mHandler.handleMessage("傳遞標志,實現Toast的顯示");
                      }
                  }.start();
                  button.setEnabled(false);
                  button.setText("加關注");
                  Toast.makeText(getBaseContext(), "取消關注成功", Toast.LENGTH_SHORT).show();
                  flag=0;
                  }


       }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.progress_dialog, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

還有怎麼不能這樣寫:

 Handler mHandler=new Handler();
    class Handle
    {
    public void handleMessage(Message msg)
    {
        super.handleMessage(msg);
        button.setEnabled(false);
        button.setText("加關注");
        Toast.makeText(getBaseContext(), "取消關注成功", Toast.LENGTH_SHORT).show();
    }
    }

最佳回答:


 package com.example.prodialog;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    private Button button;
    public ProgressDialog dialog;
    int flag, count;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setEnabled(true);
        button.setText("加關注");
        button.setBackgroundColor(Color.parseColor("#F5F5DC"));
        flag = 0;
        button.setOnClickListener(this);
    }

    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            button.setEnabled(true);
            button.setText("加關注");
            button.setBackgroundColor(Color.parseColor("#F5F5DC"));
            dialog.dismiss();
            Toast.makeText(getBaseContext(), "取消關注成功", Toast.LENGTH_SHORT)
                    .show();
        }
    };

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            button.setEnabled(true);
            button.setText("已關注");
            button.setBackgroundColor(Color.parseColor("#F8F8FF"));
            dialog.dismiss();
            Toast.makeText(getBaseContext(), "關注成功", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            if (flag == 0) {
                dialog = ProgressDialog.show(MainActivity.this, "", "關注中...",
                        true);
                count = 0;
                new Thread() {
                    public void run() {
                        try {
                            while (count <= 10) {
                                dialog.setProgress(count++);
                                Thread.sleep(1000);
                            }
                            dialog.cancel();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            dialog.dismiss();
                        }
                    }
                }.start();
                handler.sendEmptyMessageDelayed(0, 1000);
                flag = 1;
            } else if (flag == 1) {
                dialog = ProgressDialog.show(MainActivity.this, "", "取消關注中...",
                        true);
                count = 0;
                new Thread() {
                    public void run() {
                        try {
                            while (count <= 10) {
                                dialog.setProgress(count++);
                                Thread.sleep(1000);
                            }
                            dialog.cancel();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            dialog.dismiss();
                        }
                    }
                }.start();
                mHandler.sendEmptyMessageDelayed(0, 1000);
                flag = 0;
            }
        }
    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved