怎么一打开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();
    }
    }

4个回答

 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;
            }
        }
    }

}

程序一运行就出现取消关注中是因为你在oncreate函数里让progressdialog显示了,所以一打开app就有。如果你要用自己自定义的class,至少得继承人家的handler类,不过还是没必要这么做,多此一举

你在onCreate()方法中写了这句
dialog=ProgressDialog.show(ProgressDialogActivity.this,"","取消关注中...",true);
所以一打开程序就是取消关注中

YXTS122
YXTS122 回复流浪_过客:谢谢你完美解决我的问题,元旦快乐!
接近 3 年之前 回复
as_csdn
流浪_过客 回复YXTS122: 我改了一下你的代码,基本实现了你的要求,你看看是不是这种效果,代码我粘贴在下面
接近 3 年之前 回复
as_csdn
流浪_过客 回复YXTS122: 就是你下面粘贴的代码吗?
接近 3 年之前 回复
YXTS122
YXTS122 刚刚修改了一下代码,运行有点问题,能再帮我看看吗?拜托了
接近 3 年之前 回复

按一下加关注,只出现progressdialog,不出现toast,按钮上的文字也不更改为"已关注"
图片说明
再按一下加关注,只出现toast,不出现progressdialog,按钮上的文字也不更改

 package com.example.ch02_progressdialogdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
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,count;                                     
    @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("加关注");
        button.setBackgroundColor(Color.parseColor("#F5F5DC"));
        flag=0;
        button.setOnClickListener(this);
    }

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

    }

    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.bt1:
              if (flag==0)
              {
              dialog=ProgressDialog.show(ProgressDialogActivity.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();
              /*button.setEnabled(true);
            button.setText("已关注");
            button.setBackgroundColor(Color.parseColor("#F8F8FF"));
            dialog.dismiss();
            Toast.makeText(getBaseContext(), "关注成功", Toast.LENGTH_SHORT).show();

              */flag=1;
              }
              else if (flag==1)
              {
                  dialog=ProgressDialog.show(ProgressDialogActivity.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();
                          }
                          //mHandler.handleMessage("传递标志,实现Toast的显示");
                      }
                  }.start();
                  button.setEnabled(true);
                button.setText("加关注");
                button.setBackgroundColor(Color.parseColor("#F5F5DC"));
                dialog.dismiss();
                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);
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!