YXTS122 2016-12-30 22:27 采纳率: 100%
浏览 1174
已采纳

怎么一打开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条回答 默认 最新

  • 代码的灵魂是bug! 2017-01-01 05:19
    关注
     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;
                }
            }
        }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题