如何在警报对话框中使用凌空?

时间:2016-04-26 06:03:55

标签: android android-volley android-alertdialog

这里让我解释一下我的问题需要在alertdialog里面添加凌空,我已经成功完成它就像ping了输入的Url一样工作正常但是我需要做的是onErrorResponse方法警告对话框获取隐藏假设当用户输入错误的url它将继续使用onErrorResponse方法我的alertdialog获取隐藏需要保持唤醒对话框,即使发生错误到目前为止,我试过的是:

这是我的代码:

   final     AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("Add Self Hosted URL "); //Set Alert dialog title here
        alert.setMessage("Enter The Url Here"); //Message here
        final       EditText input = new EditText(context);
        alert.setView(input);
    ConnectivityManager cn = (ConnectivityManager) PingingActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo nf = cn.getActiveNetworkInfo();
        if (nf != null && nf.isConnected()) {

           final  ProgressDialog progressDialog=new ProgressDialog(this);
        progressDialog.hide();

            Toast.makeText(getApplicationContext(), "Network Available", Toast.LENGTH_LONG).show();
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, int whichButton) {
                    progressDialog.show();
                    progressDialog.setMessage("Pinging Please Wait");
                    final String srt = input.getEditableText().toString();
                    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
                    StringRequest stringRequest = new StringRequest(Request.Method.GET, srt,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {

                                    Log.e("resp", response);
                                    SharedPreferences settings = getSharedPreferences(PingingActivity.id, 0); // 0 - for private mode
                                    SharedPreferences.Editor editor = settings.edit();
                                    editor.putString("Url", srt);
                                    editor.putBoolean("URLflag", true);
                                    editor.apply();
                                    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                                    startActivity(intent);
                                }
                            }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            //  alert.show();

                            //       input.setText(srt);
                            //final String srt = input.getEditableText().toString();
                            String str = error.toString();
                            if (str != null) {
                                Toast.makeText(getApplicationContext(), "Please Check the Entered URL", Toast.LENGTH_SHORT).show();
//                                  progressDialog.cancel();

                            }
                        }
                    });
// Add the request to the RequestQueue.

                    queue.add(stringRequest);
                    stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
                            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

                    Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
                }
            });
            alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                    dialog.cancel();
                }
            }).setIcon(R.drawable.ic_info_black_24dp).show(); //End of alert.setNegativeButton

       /* Alert Dialog Code End*/

        }


        else {
            showAlertDialog("No Network", "Please Check Your Network Connectivity", true);
        }

即使在onerrorresponse需要显示我的alertdialog我怎么能实现这一点。这可能是一个愚蠢的问题,但作为一个初学者我正在努力解决这个问题,任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

创建一个返回状态的bool方法,将其命名为

  

alert.setPositiveButton(&#34; OK&#34;,new DialogInterface.OnClickListener()

从响应返回true,从onError()

返回false

如果响应返回成功,则启动所需的Activity,否则显示警告对话框。