Android自定义Dialog


  • 效果图

test_net.png
  • AndroidManifest.xml(记得加上权限)


  • style.xml


  • layout.xml




        

            
        

  • NetView.java


  public class NetView implements OnKeyListener {
    private AlertDialog exitDialog;
    private boolean isShowing;
    private TextView netText;
    private static NetView instance = null;

    public NetView(Context context) {
        exitDialog = new AlertDialog.Builder(context, 
                                            R.style.NetDialogBackground).create();
    }

    public static synchronized NetView getInstance(Context context) {
        if (instance == null) {
            instance = new NetView(context);
        }
        return instance;
    }

    public void showNetDialog() {
            exitDialog.getWindow().setType(WindowManager.
                                                LayoutParams.TYPE_SYSTEM_ALERT);  
            exitDialog.show();
            Window window = exitDialog.getWindow();
            window.setContentView(R.layout.broken_net_tips_view);
            netText = (TextView) exitDialog.findViewById(R.id.net_text);
            netText.requestFocus();
            netText.setOnKeyListener(this);
    }

    public boolean isShowing() {
        return exitDialog.isShowing();
    }

    public void exitDilog() {
        exitDialog.dismiss();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && 
                                      event.getAction() == KeyEvent.ACTION_DOWN) {
            exitDilog();
        }
        return false;
    }
}
  • Test.java

NetView .getInstance(context).showNetDialog();//显示dialog
NetView .getInstance(context).exitDilog();//关闭dialog  

欢迎加入QQ群和关注微信公众号

请不要重复添加
一群:259182457       二群:471496394
三群:137038987       四群:256636915

Android自定义Dialog_第1张图片
IT专栏微信公众号.jpg

你可能感兴趣的:(Android自定义Dialog)