1、Android 适应任何样式 提示弹出框 Dialog 封装在 Android 开发中,难免有各种各样的提示框,如加载数据等待框,删除确认框、输入密码提示框等等,这些是完全可以自定义的,这里给出一个框架以及一个示例,帮助你开发任何样式布局的提示框,废话不多说,直接贴代码:HintDialog.javaimport android.app.AlertDialog;import android.app.Dialog;import android.content.Context;import android.view.KeyEvent;import android.view.Window;publi
2、c class HintDialogDialog mDialog = null;private Context mContext = null;private IHintDialog mIDialogInstance = null;/* 构造函数* param context*/public HintDialog(Context context)mContext = context;mDialog = new AlertDialog(mContext)Overridepublic boolean onKeyDown(int keyCode, KeyEvent event)if (keyCode
3、 = KeyEvent.KEYCODE_BACK return true;return super.onKeyDown(keyCode, event);mDialog.setCancelable(false);mDialog.setCanceledOnTouchOutside(false);/* * param iLayoutResId 此DIALOG采用的布局文件* param interfaceInstance 此DIALOG 需要实现的一些接口事件*/public void showDialog(int iLayoutResId, IHintDialog interfaceInstanc
4、e)if (mDialog = null | iLayoutResId = 0)return;mIDialogInstance = interfaceInstance;mDialog.show();mDialog.setContentView(iLayoutResId);Window window = mDialog.getWindow();if (mIDialogInstance != null)mIDialogInstance.showWindowDetail(window);/* 使dialog 消失*/public void dismissDialog()if (mDialog !=
5、null /* 确定Dialog 是否正在显示* return*/public boolean isShowing()if (mDialog != null return false;public interface IHintDialogpublic void onKeyDown(int keyCode, KeyEvent event);public void showWindowDetail(Window window);HintDialogActivity.javaimport com.example.hintdialogdemo.HintDialog.IHintDialog;impor
6、t android.os.Bundle;import android.app.Activity;import android.view.KeyEvent;import android.view.Menu;import android.view.View;import android.view.Window;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class Hint
7、DialogActivity extends Activity implements OnClickListenerButton btnConfirm = null;HintDialog mDialog = null;Overridepublic void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.activity_hint_dialog);btnConfirm = (Button) findViewById(R.id.button1);btnCon
8、firm.setOnClickListener(this);Overridepublic boolean onCreateOptionsMenu(Menu menu)getMenuInflater().inflate(R.menu.activity_hint_dialog, menu);return true;Overridepublic void onClick(View v)switch (v.getId()case R.id.button1:mDialog = new HintDialog(HintDialogActivity.this);mDialog.showDialog(R.lay
9、out.confirm_dialog, new IHintDialog()Overridepublic void showWindowDetail(Window window)TextView txvTitle = (TextView) window.findViewById(R.id.txvTitle);txvTitle.setText(“Confirm?“);Button btnOK = (Button) window.findViewById(R.id.btnOK);btnOK.setText(“Yes“);btnOK.setOnClickListener(HintDialogActiv
10、ity.this);Button btnCancel = (Button) window.findViewById(R.id.btnCancel);btnCancel.setText(“NO“);btnCancel.setOnClickListener(HintDialogActivity.this);Overridepublic void onKeyDown(int keyCode, KeyEvent event)if (keyCode = KeyEvent.KEYCODE_BACK)mDialog.dismissDialog();Toast.makeText(HintDialogActiv
11、ity.this,“onKeyback now“, Toast.LENGTH_LONG).show(););break;case R.id.btnOK:mDialog.dismissDialog();Toast.makeText(HintDialogActivity.this,“Confirm OK now“, Toast.LENGTH_LONG).show();break;case R.id.btnCancel:mDialog.dismissDialog();Toast.makeText(HintDialogActivity.this,“Confirm Cancel now“, Toast.LENGTH_LONG).show();break;default:break;其实也不难,主要是 AlertDialog 的使用,注意需要显示调用 dissmissDialog()来消失此弹出框。