怎么让它先显示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);
}
}