怎麼讓它先顯示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);
}
}
建議你通過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);
}