YXTS122 2016-12-29 22:22 采纳率: 100%
浏览 2178
已采纳

怎么实现按一下"已关注"变成"加关注"?

怎么让它先显示progressdialog,再显示toast,不要像图中那样同时显示
还有怎么实现按一下"已关注",按钮上的文字变成"加关注"?
图片说明
ProgressDialogActivity.java

 package com.example.ch02_progressdialogdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
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);
    }

    @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();
                          }
                      }
                  }.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);
    }
}

  • 写回答

3条回答

  • Exploring1024 2016-12-30 01:13
    关注

    建议你通过ProgressDialog的处理事件回调进行处理,当关注总执行完毕后,发送个消息,然后弹出来Toast。为什么你线程延迟了没有效果。

                   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();
    

    你的这段代码中,你new Thread。你只是对子线程进行延迟,并没有处理主线程的延迟。你的这个延迟对子线程有效果,但是MainThread是无用的。

        private Handler mHandler = new Handler(){
    
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                 button.setEnabled(false);
                 button.setText("加关注");
                 Toast.makeText(getBaseContext(), "取消关注成功", Toast.LENGTH_SHORT).show();
            }
    
        };
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
             dialog=ProgressDialog.show(ProgressDialogActivity.this,"","取消关注中...",true);
             mHandler.sendEmptyMessageDelayed(0, 3000);
        } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题