工具类:
public class DialogUtil {
@SuppressLint("InlinedApi")
public static Dialog createLoadingDialog(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.progress_bar, null);
LinearLayout layout = (LinearLayout) v.findViewById(R.id.layout);
Dialog loadingDialog = new Dialog(context, R.style.loading_dialog_tran);
loadingDialog.setCanceledOnTouchOutside(false);// true点击对话框外部取消对话框显示
loadingDialog.setCancelable(false);// 不可以用“返回键”取消
loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
return loadingDialog;
}
}
设置基类,添加调用方法:
package com.example.dialogdemo;
import android.app.Dialog;
import android.support.v7.app.ActionBarActivity;
public class baseActionBarActivity extends ActionBarActivity {
/**
* 加载图片提示框
*/
private Dialog progressDialog;
/**
* 显示dialog
*/
public void showProgressDialog() {
try {
if (progressDialog == null) {
progressDialog = DialogUtil.createLoadingDialog(this);
}
progressDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏dialog
*/
public void dismissProgressDialog() {
try {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* dialog是否显示
*/
public boolean isShow() {
try {
if (progressDialog != null && progressDialog.isShowing()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
首页显示:
package com.example.dialogdemo;
import android.support.v7.app.ActionBarActivity;
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;
public class MainActivity extends baseActionBarActivity implements OnClickListener {
private Button show, dismiss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (Button) findViewById(R.id.show);
show.setOnClickListener(this);
dismiss = (Button) findViewById(R.id.dismiss);
dismiss.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.show:
showProgressDialog();
break;
case R.id.dismiss:
dismissProgressDialog();
break;
default:
break;
}
}
}