2015-11-04 44 views
-6

我想创建一个类似于此图像对话框,谁能帮我出enter image description here自定义对话框因为没有上网

+1

一切有关对话框(包括插图和代码):http://developer.android.com/intl/es/guide/topics/ui/dialogs.html#CustomLayout –

回答

1

您可以创建一个警告对话框来做到这一点。像这样的东西应该工作。

AlertDialog dialog = new AlertDialog.Builder(mContext) 
     .setTitle("Connection Failed") 
     .setMessage("Please Check Your Internet Connection") 
     .setPositiveButton("Try Again", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        //Code for try again 
       } 
      }) 
     .setNegativeButton("Exit", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 

       } 
      }).create(); 
dialog.show(); 
0

第一你需要检查连接,为此添加下面的类。 类ConnectionDetector:

public class ConnectionDetector { 

    private Context _context; 

    public ConnectionDetector(Context context) { 
     this._context = context; 
    } 

    public boolean isConnectingToInternet() { 
     ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity != null) { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) 
       for (int i = 0; i < info.length; i++) 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
         return true; 
        } 

     } 
     return false; 
    } 

} 

然后检查ConnectionDetector的结果,并显示警告对话框。