【问题标题】:Dialog Box not working [closed]对话框不起作用[关闭]
【发布时间】:2014-01-02 08:56:26
【问题描述】:

过去 3 小时我一直在尝试实现此对话框,但我不知道为什么它没有弹出,我认为最好让全班同学了解我的问题:

Registration.java

public class Registration extends Activity {





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
    }






    //on submit press
    public void SubmitRegistration(View view) {



        // start an asynch request
        class RequestTask extends AsyncTask<String, String, String>{





            @Override
            public String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (IOException e) {
                    //TODO Handle problems..
                }
                return responseString;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                //Do anything with response..
            }
        }










           // assign text in fields to string values
           EditText first = (EditText)findViewById(R.id.first);
           String first2 = first.getText().toString();

           EditText last = (EditText)findViewById(R.id.last);
           String last2 = last.getText().toString();

           EditText display = (EditText)findViewById(R.id.display);
           String display2 = display.getText().toString();
           //calculates the number of characters in the display field
           int length2 = display2.length();


           EditText email = (EditText)findViewById(R.id.email);
           String email2 = email.getText().toString();


           EditText password = (EditText)findViewById(R.id.password);
           String password2 = password.getText().toString();


           EditText vpassword = (EditText)findViewById(R.id.vpassword);
           String vpassword2 = vpassword.getText().toString();
           //calculates the number of characters in the password field
           int length = vpassword2.length();









           // verifying the following in order:  Passwords match? A Password field is empty?  
           //Password and Display Name less than 6 characters long? Email contains an @ sign and a period?   
           if(!vpassword2.equals(password2)) {
           Toast.makeText(getApplicationContext(), "Passwords do not match!", Toast.LENGTH_SHORT).show(); 


           }
           else if (password2.isEmpty() || vpassword2.isEmpty()){

           Toast.makeText(getApplicationContext(), "Password field is empty", Toast.LENGTH_SHORT).show(); 
           }
           else if (length < 6 || length2 < 6 ) {
           Toast.makeText(getApplicationContext(), "Password and Display Name must be at least 6 characters long", Toast.LENGTH_LONG).show(); 
           }

           else if (!email2.contains("@") || !email2.contains(".")){
           Toast.makeText(getApplicationContext(), "Must enter valid email address.", Toast.LENGTH_SHORT).show(); 
           }
           //start else
           else {

           //send php with all the data to server for validation and insertion into table
           String output = null;
                try {
                output = new RequestTask()
                .execute("http://www.alkouri.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
                .get();
                //example: www.alkouri.com/android/registercheck.php?first=Adam&last=Alkouri&display=arugala&email=arugala@blackbaud.com&password=123
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }





                //if the response from website contains "duplicate" (which means there is a duplicate email address in the DB) then it will display toast.
          if (output.contains("Duplicate")) {
          Toast.makeText(getApplicationContext(), "Email address already in system, press back button if you forgot password and click on Forgot Password ", Toast.LENGTH_LONG).show(); 

          }
          //if the response from website contains "You have registered successfully" then it will send popup message and go to login screen 
          else if (output.contains("You have")){




         //start dialogue
         AlertDialog.Builder alertbox = new AlertDialog.Builder(Registration.this);
         alertbox.setMessage("You have succesfully registered.  Please check your email for further instructions."); // Please Restart Application // "Please restart the app and download your purchase again".
         alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            //create button in dialogue
            public void onClick(DialogInterface arg0, int arg1) 
            {
            //on clicking "ok" in the dialogue box, current activity will close and return to last activity (login screen).
            finish();               

            }
         });
         alertbox.show();
          }     
            }//end else







     }  //end button click task
}//end class

所以,在最底部,就在最后一个 toast 的正下方,我想弹出一个对话框。
这是为了让用户知道他们已经注册,他们应该查看他们的电子邮件以获取更多说明。

toast 显示得很好,但是当我实现以下任何示例时,什么都没有弹出。
我在 LogCat 中没有收到任何错误,只是没有弹出任何内容....

I have tried the following examples that I Have seen online:

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

http://stackoverflow.com/questions/2115758/how-to-display-alert-dialog-in-android

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
            builder1.setMessage("Write your message here.");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder1.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert11 = builder1.create();
            alert11.show();

我做错了什么?

【问题讨论】:

  • 请正确格式化您的代码
  • 看看我的回答。你的代码太庞大了。你在点击监听器中组合了所有东西,这会导致很多混乱和错误。

标签: java android dialog android-alertdialog


【解决方案1】:
AlertDialog.Builder alertbox = new AlertDialog.Builder(MainMenuActivity.this);
        alertbox.setMessage("Please restart the app and download your data again."); // Please Restart Application // "Please restart the app and download your purchase again".
        alertbox.setPositiveButton("YES", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                    //code
            }
        });

        alertbox.setNegativeButton("NO", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //code  
            }
        });

        alertbox.show();

【讨论】:

  • 谢谢,这很有魅力。
【解决方案2】:

您需要在 onCreate() 方法中为您的提交按钮放置一个 OnclickListener 并从那里调用您的其他函数。

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    submit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            //Call your submitregistration function function here                
        }
    });
}

【讨论】:

    【解决方案3】:
     output = new RequestTask()
    .execute("http://www.mywebsite.com/android/registercheck.php?first=" + first2 + "&last=" + last2 + "&display=" + display2 + "&email=" + email2 + "&password=" + password2)
    .get();
    

    调用get 不再使其异步。它阻塞了等待结果的ui线程。

    AsyncTask 移出onClick

    class RequestTask extends AsyncTask<String, String, String>{
    

    http://developer.android.com/reference/android/os/AsyncTask.html#get()

    public final Result get ()
    
    Added in API level 3
    Waits if necessary for the computation to complete, and then retrieves its result.
    

    还将您的变量声明为实例变量并在onCreate 中初始化您的视图。

    EditText first ;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    first = (EditText)findViewById(R.id.first);
    

    点击按钮从editText获取文本

    调用异步任务

     new RequestTask().execute(params); // no get
    

    doInbackground 计算的结果是onPostExecute 的参数。根据结果​​,您可以在onPostExecute 中显示对话框。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2014-08-02
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多